Solved

Python script is not giving desired output

Posted on 2013-11-11
2
327 Views
Last Modified: 2013-11-12
Hi Guys,

I'm having an issue with a Python learning program I have written. I have refractored all my code but when I run the program with the if __main == "__main__" it says main not defined eventhough i have a function definition called main. How can I fix it? Also I'm having and issue after I create my newTrip object and want the program to run the say_hi function. All i get when i run my code is

Welcome to the Program!
<bound method Trip.say_hi of <__main__.Trip object at 0x7fcac3c4c590>>

Open in new window


My full code is below:

#!/usr/bin/python

class Trip(object):
	def __init__(self, milePerGallon, distance, pricePerGallon):
		self.milesPerGallon = milePerGallon
		self.distance = distance
		self.pricePerGallon = pricePerGallon

	def calculated_price_per_gallon(self):
		return (self.pricePerGallon * self.distance)

	def round_trip(self):
		return (self.distance * 2)

	def say_hi(self, askName):
		self.askName = raw_input("What is your name?")

	def read_float(self, mpg_text):
	#def read_float(self, mpg_text):
		mpg_text = "Vehicles Advertised Miles Per Gallon: "
		value = float(raw_input("Please enter: " +mpg_text))
		print ('%s is %f') % (self.mpg_text, self.value)
		return value

		#mpg_text = "Vehicles Advertised Miles Per Gallon: "
		#value = float(raw_input("Please enter: " +mpg_text))
		#print ('%s is %f') % (self.mpg_text, self.value)
		#return value
	def main(self):
		print "Welcome to the Program!"
		
		



#if __name__ == "__main__":
newTrip = Trip(22, 13.5, 3.27)
newTrip.main()
print newTrip.say_hi

Open in new window

0
Comment
Question by:Tbalz
2 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 39640555
This output:
<bound method Trip.say_hi of <__main__.Trip object at 0x7fcac3c4c590>> 

Open in new window


Is caused by this line:
print newTrip.say_hi

Open in new window



The 'say_hi' is a method of your newTrip object which is what your print is basically telling you.  To execute the method, you need to pass its parameter list to it:
print newTrip.say_hi('whatever you\'re attempting to do with askName')

Open in new window


I'm not sure what you're trying to do with the askName parameter of that method.  You're not actually using it within the method so I'm guessing you were just trying things.  The method probably should be:
    def say_hi(self):
        self.askName = raw_input("What is your name?")
        return self.askName

Open in new window


and assuming newTrip is an instance of your Trip class then you'd call it like this:
print newTrip.say_hi()

Open in new window


As far as your problem with main not being defined, glancing I don't see a problem with the code as you posted it.  If you post the code that is actually giving you that error, we'll probably be able to help a bit more.
0
 

Author Closing Comment

by:Tbalz
ID: 39643108
Wow excellent explanation! Probably the best I ever received! MANY THANKS!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

809 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