Now we want to display which error was thrown in our voting program. ( BOTH QUESTIONS ARE PYTHONS PROGRAMS)
# Set the variable
age = input("What is your age?")
# Insert a try/except statement
# here when you convert the input
# to a number using int()
try:
age = int(age)
if age >= 18:
print("Go vote!")
except:
print("Please enter a valid age!")
Output:
invalid literal for int() with base 10: 'old' Please enter a valid age!
We're working with pie now. Our code is already catching when the user does not enter a valid integer, but we need to add default handling to catch everthing else.
# Get Input
pieces = input("How many pieces of pie do you want? ")
# Attempt to convert to integer
try:
percentage = 1/int(pieces)
except ValueError:
print("You need to enter a number!")
Output:
Something went wrong!
Answer:-
Your Code
Here is the same output that you want...
Output
When - Enter - 18
When Enter - Old
If you have any question feel free to ask....
Best of luck...
Get Answers For Free
Most questions answered within 1 hours.