Page 289 - CTS - CSA TP - Volume 2
P. 289
COMPUTER SOFTWARE APPLICATION - CITS
TASK 2: Invalid Input
Code:
# Example 2: Invalid Input
try:
number=int(input(“Enter an integer: “))
exceptValueError:
print(“Error: Invalid input. Please enter an integer.”)
else:
print(f”Entered number: {number}”)
finally:
print(“Input handling completed.”)
Explanation:
• Attempt to convert user input to an integer.
• exceptValueError catches the exception if the input is not a valid integer.
• else block executes if the input is a valid integer.
• finally block always executes.
Output:
274
CITS : IT & ITES - Computer Software Application - Exercise 131