any help would be greatly appreciated.
- Code: Select all
def miles_to_km(miles):
return miles * 1.6
def read_miles(prompt):
miles = float(input(prompt))
if miles < 0: raise ValueError()
return miles
def read_miles_retry(prompt, retries = 3):
while retries > 0:
try:
return read_miles(prompt)
except:
retries -= 1
print('Invalid input')
raise ValueError()
#---------------------------------------------------------------
def fah_to_cel(temp):
return (temp - 32) * 5/9
def read_temp(prompt):
temp = float(input(prompt))
if temp > 1000: raise ValueError()
return temp
def read_temp_retry(prompt, retries = 3):
while retries > 0:
try:
return read_temp(prompt)
except:
retries -= 1
print('Invalid input')
raise ValueError()
#----------------------------------------------------------------
def gal_to_lit(gallon):
return gallon * 3.9
def read_gallon(prompt):
gallon = float(input(prompt))
if gal < 0 : raise ValueError()
return gallon
def read_gallon_retry(prompt, retries = 3):
while retries > 0:
try:
return gallon(prompt)
except:
retries -= 1
print('Invalid input')
raise ValueError()
#-------------------------------------------------------------------
def miletokm():
try:
mtk = miles_to_km(read_miles_retry('how many miles would\
you like to convert? '))
print( format (mtk, '.2f'))
except:
exit()
def fahtocel():
try:
ftc = fah_to_cel(read_temp_retry('how many degrees in Fahrenhiet would\
you like to convert to celsius? '))
print( format (ftc, '.1f'))
except:
exit()
def gallontoliter():
try:
gtl = gal_to_lit(read_gallon_retry('how many gallons would you\
like to convert to liter? '))
print( format (gtl, '.2f'))
except:
exit()
def main():
miletokm()
fahtocel()
gallontoliter()
exit()
main()