Solved

Learning Python and stuck on script

Posted on 2013-11-07
2
453 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 74

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now