Reputation: 13
Problem: When driving on a trip I often notice indicating the number of miles to some city or town and wonder how long before I will get there. Produce code that will accept my speed and the distance I need to travel and indicate how long (in minutes) it will take me to get there. (Remember that MPH tells how many miles I can travel in 60 minutes.)
Here is the code I have produced so far. I believe the issue is in line 5 or 6 but I'm stumped. I'm able to enter in my MPH (70) and my distance (120). My answer should be 102.857..... but it won't compute.
answer1 = input("Please enter the speed you will be traveling in MPH: ")
mph = int(answer1)
answer2 = input("Please enter the distance you will be traveling: ")
distance = int(answer2)
time = float((answer2 / answer1) * answer1)
print("That will take " +str(time) + " minutes.")
Upvotes: 0
Views: 391
Reputation: 366
It is python problem. When you divide with int. it may give you wrong result. Please make sure that your every input is float type. it will help you to get your desired answer.
Upvotes: 0
Reputation: 3399
There were some problems with your code, namely trying to divide a string and an integer and using the wrong formula for the time calculation. Here's the code with the issues fixed.
answer1 = input("Please enter the speed you will be traveling in MPH: ")
mph = float(answer1)
answer2 = input("Please enter the distance you will be traveling: ")
distance = float(answer2)
time = float((distance / mph) * 60)
print("That will take " +str(time) + " minutes.")
Output:
Please enter the speed you will be traveling in MPH: 70
Please enter the distance you will be traveling: 120
That will take 102.85714285714285 minutes.
Upvotes: 2
Reputation: 308
when you read an input from the terminal it is read in as a string. You are converting the answer1
and answer2
to int and storing them in mph
and distance
respectively but use the original strings for computation. Use the following code for the 5th line.
time = float((distance / mph) * mph)
Upvotes: 0