Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Learning Python and stuck on script

Posted on 2013-11-07
2
Medium Priority
?
478 Views
Last Modified: 2013-11-08
Hi Guys,

I have started to learn Python and I'm completely lost. I wanted to create an object oriented script that would calculate trip cost but I'm getting the wrong output. Can someone please tell me what I'm doing wrong and how i can correct my program below? When I try to get the return of my function Mileageprice_per_gallon I get some weird output like:

<bound method TripCost.Mileageprice_per_gallon of <__main__.TripCost instance at 0x7f28fe0701b8>>

Open in new window


#!/usr/bin/python

class TripCost:
	def __init__(self, miles_per_gallon, distance, price_per_gallon):
		self.miles_per_gallon = miles_per_gallon
		self.distance = distance
		self.price_per_gallon = price_per_gallon
	def Mileageprice_per_gallon(self):
		return (price_per_gallon * distance)


trip1 = TripCost(22, 13.5, 3.27)
print trip1.Mileageprice_per_gallon

Open in new window

0
Comment
Question by:Tbalz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1800 total points
ID: 39632223
Add parentheses to the function call.

e.g.

print trip1.Mileageprice_per_gallon()

Open in new window


Also, you need to include self when referencing the class-level fields within your Mileageprice_per_gallon method.

e.g.

def Mileageprice_per_gallon(self):
		return (self.price_per_gallon * self.distance)

Open in new window

0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 200 total points
ID: 39632833
Also, use 4 spaces for one indentation level. I suggest not to use tab for indentation. Your editor can be set to use spaces instead of tabs, or to indent by spaces.

As kaufmed wrote, the self must be used explicitly.

If not forced to use Python 2.x, you should probably start with Python 3.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Make the most of your online learning experience.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question