?
Solved

Learning Python and stuck on script

Posted on 2013-11-07
2
Medium Priority
?
472 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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

770 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