[Tutor] Miles Per Gallon Calculator`

W W srilyk at gmail.com
Thu Sep 18 12:51:26 CEST 2008


On Thu, Sep 18, 2008 at 5:14 AM, <iannaconec at optonline.net> wrote:

> When <variable> = raw_input ( " please enter a number " )
> <variable> = int  (<variable>) is run as part of a python script It should
> take in the user stored input and store it as an integer. Am I correct. The
> string represented by <variable> in memory is now represented by 5 for
> example?
> If this is correct why do I get an error regarding the division of two
> strings if i type print " < variable> " / < variable>


well if you have something like this:

miles = int("5")
gallons = 1
print "miles"/gallon

the " " around miles turns miles into a string. And you just can't do that.
Here's a proper example of an mpg calculator:

def mpg():
    miles = int( raw_input("Miles driven: "))
    gallons = int( raw_input("Gallons used: "))
    print "Your Miles Per Gallon: ", miles/gallons

although converting them to float would allow you a little more accuracy in
your measurements. But if you're just looking at whole number values, that
should be fine.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080918/fb192f8e/attachment.htm>


More information about the Tutor mailing list