Solved

Python script is not giving desired output

Posted on 2013-11-11
2
319 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
Comment Utility
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
Comment Utility
Wow excellent explanation! Probably the best I ever received! MANY THANKS!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is an explanation of a simple data model to help parse a JSON feed
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

11 Experts available now in Live!

Get 1:1 Help Now