Solved

Learning Python and stuck on script

Posted on 2013-11-07
2
461 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 28

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

786 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