Trying to use Python to create a While statement for a Travel Program -
#program calculate cost of gas on trip #created sylvia mccoy, march 31, 2015 #created in python #declare variables trip_destination = 0 number_miles = 0 current_mpg = 0.0 gallon_cost = 0.0 total_gallons = 0.0 total_cost = 0.0 #loop enter destinations while true: trip_destination = input("enter travel: ") number_miles = float(input("enter how many miles destination: ")) gallon_cost = float(input("enter how gallon of gas costs: ")) current_mpg = float(number_miles / gallon_cost) print("your trip to: " + trip_destination) print("your total miles: " + number_miles) print("your mpg: " + current_mpg) error in line 20 , 21...stated earlier float, how them print? have work in python. thanks!
you can't use + operator combine string float.
it can used combine 2 of single type (eg.2 strings, 2 floats, etc.)
with said, desired output, can simplify conversions need 1 line, so:
trip_destination = 0 number_miles = 0 current_mpg = 0.0 gallon_cost = 0.0 total_gallons = 0.0 total_cost = 0.0 #loop enter destinations while true: trip_destination = input("enter travel: ") number_miles = input("enter how many miles destination: ") gallon_cost = input("enter how gallon of gas costs: ") current_mpg = str(float(number_miles) / float(gallon_cost)) print("your trip to: " + trip_destination) print("your total miles: " + number_miles) print("your mpg: " + current_mpg) using input() function let value user (already in form of string). way, won't have format/convert later.
Comments
Post a Comment