[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Python script is not giving desired output

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

656 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