def main():
print ('Welcome to the mpg calculator program')
print()
#get the miles driven using the input function
miles_driven = input ("Please enter miles driven: ")
miles_driven = float (miles_driven)
#get the gallons used using the input function
gallons_used = input ("Please enter gallons used: ")
galons_used = float (gallons_used)
#calculate mpg here, using the inputted values
mpg = miles_driven / gallons_used
print('The MPG based on', format(miles_driven, '.2f'),\
'miles driven and with', format(gallons_used, '.2f'), \
'gallons used is:', format(MPG, '.2f'),sep=' ')
#calls main
main()
>>>
Welcome to the mpg calculator program
Please enter miles driven: 200
Please enter gallons used: 50
Traceback (most recent call last):
File "C:\Users\Mark\Desktop\ITP 100\mpg.py", line 16, in <module>
main()
File "C:\Users\Mark\Desktop\ITP 100\mpg.py", line 11, in main
mpg = (miles_driven / gallons_used)
TypeError: unsupported operand type(s) for /: 'float' and 'str'
RHNation
0
Newbie Poster
Recommended Answers
Jump to Postgalons_used and gallons_used are 2 different variables. Typo.
Jump to PostI cleaned it up a little for you:
def main(): print ('Welcome to the mpg calculator program: \n') # get the miles driven using the input function # input() gives a string so convert with float() miles_driven = float(input ("Please enter miles driven: ")) # get the …
All 7 Replies
Gribouillis
1,391
Programming Explorer
Team Colleague
RHNation
0
Newbie Poster
Lardmeister
461
Posting Virtuoso
farmwife
0
Light Poster
woooee
814
Nearly a Posting Maven
woooee
814
Nearly a Posting Maven
farmwife
0
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.