Write a Python program that has a runtime error.
Provide the following.
The code of your program:
number = int(input('Please enter an integer:'))
Output demonstrating the runtime error, including the error message:
# python main.py
Please enter an integer:number
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'number'
An explanation of the error message: An exception can occur when the value of the variable 'number' is converted to an integer. If you enter the word “number”, you get an error: ValueError: invalid literal for int() with base 10: 'number'. Because you can't convert text to numbers.
An explanation of how to fix the error:
try:
number = int(input('Please enter an integer:'))
print('You entered', number)
except ValueError:
print('You entered invalid value')
Comments
Leave a comment