Connecting Tech Pros Worldwide Forums | Help | Site Map

new at this and need alittle help

Newbie
 
Join Date: Sep 2006
Posts: 1
#1: Sep 8 '06
I am trying to get it to look something like this if someone could give me a hint or a little help (for school) i can't get the calculations to work ( i can't get it to read 22.421875.

Enter the gallons used (-1 to end) : 12.8
Enter the miles driven: 287
The miles / gallon for this tank was 22.421875

bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#2: Sep 12 '06

re: new at this and need alittle help


Will you post you formula and the line that prints the result?
kudos's Avatar
Expert
 
Join Date: Jul 2006
Location: Norway
Posts: 114
#3: Sep 13 '06

re: new at this and need alittle help


is this what you are looking for?

galleon = raw_input("Enter the gallons used (-1 to end) : ")
if(galleon != "-1"):
miles = raw_input("Enter the miles driven:")
print "The miles / gallon for this tank was " + str(eval(miles)/eval(galleon))
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#4: Sep 13 '06

re: new at this and need alittle help


Ah ha! Your trouble is with you use of the print command.

print is a very powerful command which deservers to be studied.
print is polymorphic (meaning in knows what type of arguments it gets and adapts) so you don't need to do need to do any conversions to str.

The simple way is:

print "The miles / gallon for this tank was " , miles/galleon

There are cool formating features of print so you can do things like:

>>> print "you got %.5f miles to the gallon" %miles/galleon

which tells print to alway print a 'f'loating point number with '5' decimal places to the right of '.'.

Have fun learning python and post here for more help!

Barton
bartonc's Avatar
Moderator
 
Join Date: Sep 2006
Location: Minden, Nevada, USA
Posts: 6,400
#5: Sep 14 '06

re: new at this and need alittle help


P.S.

Since raw_input returns strings, you'll need to convert to floating point.
Since your program want numbers, the clean thing to do is like this:

miles = float(raw_input("How far did you drive? "))
Reply