161k views
14 votes
Can someone solve this for me?

Can someone solve this for me?-example-1
User Alexi Akl
by
3.4k points

1 Answer

8 votes

Answer:

def calcSpeed(distance, time):

return distance/time

def getStat(name, unit):

while True:

try:

value = float(input("Enter the "+name+" in "+unit+": "))

return value;

except:

print("You should enter a number, please try again.")

distance = getStat('distance', 'miles')

time = 0

while time == 0:

time = getStat('time', 'hours')

if (time == 0):

print("Cannot divide by zero, so enter a non-zero time")

print("Your speed was {:.1f} mph".format(calcSpeed(distance, time)))

Step-by-step explanation:

I hope you see what is happening in the code so you can give the second one a shot yourself!

BTW: I used different units, but for sure you'll be able to fix that!

Can someone solve this for me?-example-1
User Alup
by
3.6k points