2. Write your own unique Python program that has a runtime error. Do not copy the program from your textbook or the Internet. Provide the following.
The code of your program.
Output demonstrating the runtime error, including the error message.
An explanation of the error message.
An explanation of how to fix the error.
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