what I need it to do, is to terminate the program if the user enters an invalid input.
what the program does now, is it prompts the user that the input is invalid but continues to the next function.
- Code: Select all
# lab assignment 4
# standard to metric conversions
name = input ('What is your name? ')
# define limitations on input values for functions
min_mile = 0
min_gal = 0
min_pound = 0
max_fah = 1000
min_inch = 0
# define main function
def main():
print('hello',name,', today we will be doing some standard to metric conversions.')
print('The first conversion will be to convert miles to kilometers.')
MileToKm()
print ('The next conversion we will do, will be to convert Fahrenhiet to Celsius.')
FahToCel()
print ('The next conversion will be to convert gallons to liters.')
GalToLit()
print('The next conversion will be to convert pounds to kilograms.')
PoundsToKg()
print ('The last conversion will be to convert inches to cenitmeters.')
InchToCm()
print ('That concludes this segment of standard to metric conversions. Till next time.')
# ************************************************* end of main function ******************************************
# definning functions
# define mile to kilometer function
def MileToKm():
mile = float(input('What is the number of miles that you would like to convert? '))
mile_conv = mile * 1.6
if mile_conv > min_mile :
print ('The result would be', format(mile_conv,'.2f'),'kilometers.')
else:
print ("Sorry that is an invalid input.")
# define fahrenhiet to celsius function
def FahToCel():
temp = float(input('What degrees in Fahrenhiet would to like to convert to Celsius? '))
temp_conv = (temp - 32) * 5/9
if temp_conv < max_fah :
print('The result would be',format(temp_conv,'.1f'),'degrees Celsius.')
else :
print ("Sorry that is an invalid input.")
# define gallon to liter function
def GalToLit():
gallon = float(input('What is the amount of gallons that you would like to convert to liters? '))
gallon_conv = gallon * 3.9
if gallon_conv > min_gal :
print ('The result would be',format(gallon_conv,'.1f'),'liters.')
else :
print ("Sorry that is an invalid input.")
# define pounds to kilograms function
def PoundsToKg():
pounds = float(input('What is the amount of pounds that you would like to convert to kilograms? '))
pound_conv = pounds * 0.45
if pound_conv > min_pound :
print('The result would be',format(pound_conv,'.2f'),'kilograms.')
else :
print ("Sorry that is an invalid input.")
# define inch to centimeter function
def InchToCm():
inches = float(input('how many inches would you like to convert to centimeters? '))
inch_conv = inches * 2.54
if inch_conv > min_inch :
print('the result would be',format(inch_conv,'.2f'),'centimeters.')
else :
print ("Sorry that is an invalid input.")
#*************************************end of functions defined********************************************
# call for main function to run
main()
I am lost and I dont know how it should be structured. I dont have a solid example to look at to compare and see exactly what it is I am doing wrong.
any input would be greatly appreciated.
-- Sun Feb 16, 2014 5:35 pm --
I found a work around to use and that is
import sys
sys.exit("input invalid")
while that kills the program. That is technically not how the teacher wanted us to do it. we have not learned that syntax yet.
so I still am i little lost.