Question: I need help with the following: Add a menu-driven loop so that the user can perform as many conversions as they want. On each iteration
I need help with the following:
Add a menu-driven loop so that the user can perform as many conversions as they want. On each iteration of the loop they can choose if they want to convert euros to dollars, convert dollars to euros, or end the program. The second change will be to add an exception handler so that the program does not abort if the user enters invalid characters while trying to input a number. Here are some more detailed instructions: 1. Add a menu-driven loop. Be sure to include an error message if the user enters an invalid menu selection. 2. Instead of inputting the number of euros in the main function, define a separate value-returning function that reads and returns the number of euros. 3. Add a try/except statement in your function to ensure that your function will not abort if the user enters invalid characters. The error handler should set the number of euros to -1. (Note: Be sure to leave the validation loop in your main function.)
Here is my code so far:
# Program to convert Euros to US Dollars, or vice versa # 1 euro = 1.14 US dollars getEuro = 1 getDollar = 2 quit = 3
def main(): #choice loop choice = 0 while choice != quit: #display the menu. displayMenu()
#get users choice choice = int(input(' Enter your menu selection: '))
if choice == getEuro: euros = getEuro() while euros < 0: euros = getEuro() eurosToDollars(euros)
# Convert dollars to euros elif choice == 2: dollars = getDollar() while dollars < 0: dollars = getDollar() dollarsToEuros(dollars) elif choice == quit: print(' Program ended by user. ') else: print(' Invalid Menu Selection. Please try again. ') print('Thank you!') def displayMenu(): print('Please select from the following Menu') print('--------------------------------------') print('1) Convert euros to US dollars') print('2) Convert US dollars to euros') print('3) End Program') def getEuro(): try: e = float(input(' Please input the number of euros to convert: ')) except ValueError: print('ERROR: input must be positive integer') e = -1 return e
def getDollar(): try: d = float(input(' Please input the number of dollars to convert: ')) except ValueError: print('ERROR: input must be positive integer') d = -1 return d
def eurosToDollars(euros): dollars = euros * 1.14 print(format(euros, '.2f'),'euros is', format(dollars, '.2f'),'US dollars') print()
def dollarsToEuros(dollars): euros = dollars / 1.14 print(format(dollars, '.2f'),'US dollars is', format(euros, '.2f'),'euros') print() #Call the main function main()
1. my menu selection stopped working when I changed them from eurosToDollars function to get euros function...
it works like this:
# Program to convert Euros to US Dollars, or vice versa # 1 euro = 1.14 US dollars euroToDollar = 1 dollarToEuro = 2 quit = 3
def main(): #choice loop choice = 0 while choice != quit: #display the menu. displayMenu()
#get users choice choice = int(input(' Enter your menu selection: '))
#convert euros to dollars if choice == euroToDollar: euros = float(input(' Please input the number of Euros: ')) while euros < 0: print('Number of euros must be positive integer.') euros = float(input(' Please input the number of Euros: ')) eurosToDollars(euros)
# Convert dollars to euros elif choice == dollarToEuro: dollars = float(input(' Please input the number of US dollars: ')) while dollars < 0: print('Number of US dollars must be positive integer') dollars = float(input(' Please input the number of US dollars: ')) dollarsToEuros(dollars) elif choice == quit: print(' Program ended by user. ') else: print(' Invalid Menu Selection. Please try again. ') print('Thank you!') def displayMenu(): print('Please select from the following Menu') print('--------------------------------------') print('1) Convert Euros to US Dollars') print('2) Convert US Dollars to Euros') print('3) End Program') def eurosToDollars(euros): dollars = euros * 1.14 print(format(euros, '.2f'),'euros is', format(dollars, '.2f'),'US dollars') print()
def dollarsToEuros(dollars): euros = dollars / 1.14 print(format(dollars, '.2f'),'US dollars is', format(euros, '.2f'),'euros') print() #Call the main function main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
