to prevent the program crashing?
1. Use a try-except block to catch any ValueError exceptions that may occur when attempting to convert the input to an integer.
Example code:
```python
try:
grade = int(input("Enter your grade: "))
except ValueError:
print("Invalid input. Please enter a number.")
```
2. Use the isnumeric() method to check if the input string consists only of numeric characters before attempting to convert it to an integer.
Example code:
```python
grade = input("Enter your grade: ")
if grade.isnumeric():
grade = int(grade)
else:
print("Invalid input. Please enter a number.")
```
Either of these methods can be used to handle the case where the user enters a non-numeric input and prevent the program from crashing.
George is creating a program that will ask the user to input their grade in school. George wants to prevent the program crashing if the user enters letter instead of number. Which of the following can be used
1 answer