Program code:
number = input("Enter an integer: ")
number = int(number)
print("Lucky!")
An exception can occur in the third line of code when the value of the variable n is converted to an integer. If you enter the word “number”, you get an error: ValueError: invalid literal for int() with base 10: 'numder'. Because you can't convert text to numbers.
Error correction options:
try:
number = int(number)
print("Lucky!")
except:
print("Something went wrong")
try:
number = input('Enter an integer: ')
number = int(number)
print("Everything is fine. You entered the number", number)
except ValueError:
print("You did not enter an integer")
Comments
Leave a comment