Beginner at python here. My code here is working just they way I want - unless an idiot types in alphabetical characters. If so, is crashes. Can you explain in beginner speak how I can prevent that? My code is as follows:
def main():
choice ='0'
while choice =='0':
print ("-" * 30 "MENU" "-" * 30)
print ("|" " " * 19 "1. Hourly to annual pay" " " * 20 "|")
print ("|" " " * 19 "2. Annual to hourly pay" " " * 20 "|")
print ("|" " " * 19 "3. Exit" " " * 36 "|")
print ("-" * 64)
choice = input ("Choose an option 1-3: ")
if choice =="1":
hourly = input ("Enter hourly pay: ")
annual = float (hourly) * 2080
print ("Annual pay is: ")
print ("{:0.2f}".format(annual))
main()
elif choice =="2":
a = input ("Enter annual pay: ")
h = float (a) / 2080
x = round(h, 2)
print ("{:0.2f}".format(x))
main()
elif choice =="3":
print ("Program exited gracefully.")
raise SystemExit(0)
else:
print ("Unrecognized command - choose 1-3")
main()
CodePudding user response:
You can catch ValueError when trying to convert the input to a float value. I modified your code to show how it could be done:
def get_float_input(prompt: str) -> float:
try:
data = input(prompt)
data = float(data)
return data
except ValueError:
raise ValueError('Please enter a valid number')
def main():
while True:
print("-" * 30 "MENU" "-" * 30)
print("|" " " * 19 "1. Hourly to annual pay" " " * 20 "|")
print("|" " " * 19 "2. Annual to hourly pay" " " * 20 "|")
print("|" " " * 19 "3. Exit" " " * 36 "|")
print("-" * 64)
choice = input("Choose an option 1-3: ")
if choice == "1":
try:
hourly = get_float_input("Enter hourly pay: ")
except ValueError as e:
print(e)
continue
annual = hourly * 2080
print("Annual pay is: ")
print("{:0.2f}".format(annual))
elif choice == "2":
try:
a = get_float_input("Enter annual pay: ")
except ValueError as e:
print(e)
continue
h = a / 2080
x = round(h, 2)
print("{:0.2f}".format(x))
elif choice == "3":
print("Program exited gracefully.")
raise SystemExit(0)
else:
print("Unrecognized command - choose 1-3")
if __name__ == '__main__':
main()
CodePudding user response:
Best thing you can do is to use exception handling while converting the input data to float. It will catch all invalid inputs that cant be converted to float.
def main():
choice ='0'
while choice =='0':
print ("-" * 30 "MENU" "-" * 30)
print ("|" " " * 19 "1. Hourly to annual pay" " " * 20 "|")
print ("|" " " * 19 "2. Annual to hourly pay" " " * 20 "|")
print ("|" " " * 19 "3. Exit" " " * 36 "|")
print ("-" * 64)
choice = input ("Choose an option 1-3: ")
if choice =="1":
hourly = input ("Enter hourly pay: ")
try:
annual = float (hourly) * 2080
print ("Annual pay is: ")
print ("{:0.2f}".format(annual))
except:
print("Invalid Input")
main()
elif choice =="2":
a = input ("Enter annual pay: ")
try:
h = float (a) / 2080
x = round(h, 2)
print ("{:0.2f}".format(x))
except:
print("Invalid Input")
main()
elif choice =="3":
print ("Program exited gracefully.")
raise SystemExit(0)
else:
print ("Unrecognized command - choose 1-3")
main()
CodePudding user response:
I think you have to use while True
to run program until user enters 3(infinite loop). Inorder to handle this problem you can use try except statement.
So, your code needs to be like this
def main():
print ("-" * 30 "MENU" "-" * 30)
print ("|" " " * 19 "1. Hourly to annual pay" " " * 20 "|")
print ("|" " " * 19 "2. Annual to hourly pay" " " * 20 "|")
print ("|" " " * 19 "3. Exit" " " * 36 "|")
print ("-" * 64)
try:
while True:
choice = input ("Choose an option 1-3: ")
if choice =="1":
hourly = input ("Enter hourly pay: ")
annual = float (hourly) * 2080
print ("Annual pay is: ")
print ("{:0.2f}".format(annual))
elif choice =="2":
a = input ("Enter annual pay: ")
h = float (a) / 2080
x = round(h, 2)
print ("{:0.2f}".format(x))
elif choice =="3":
print ("Program exited gracefully.")
raise SystemExit(0)
except ValueError:
print ("Unrecognized command - choose 1-3")
main()