Solved

Learning Python and stuck on script

Posted on 2013-11-07
2
463 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
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 450 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 50 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…

789 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