?
Solved

Python script is not giving desired output

Posted on 2013-11-11
2
Medium Priority
?
332 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
[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 25

Accepted Solution

by:
clockwatcher earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Introduction to Processes
Suggested Courses

765 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