Solved

Python script is not giving desired output

Posted on 2013-11-11
2
324 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VMware Workstation 12 Player 16 75
Run same command on multiple files in Linux 3 32
maybe no no httpd.conf 6 48
Getting the NAO robot to play soccer 1 17
A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

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