Definition:
When you write a program and use the raise keyword, it generates errors and displays them on the screen if an error occurs.
Now you will clearly understand the code logic using the flowchart.
Raise Keyword Flowchart:
First step: → You will write the program.
↓
Second step: → You will enter the input (word = INDIA or not)
↓
Third step: → Now check the condition (word == "INDIA" ?)
If the condition is False (No), then
↓
Raise Error (ValueError)
↓
Program stops
If the condition is True (Yes), then
↓
Execute Code
↓
Print Correct Word
↓
End the program
Syntax:
raise ErrorType("message")
Example:
word = input("Enter word (INDIA): ")
if word != "INDIA":
raise ValueError("Only INDIA is allowed")
print("Correct word:", word)
Output:
Exercise
Write a Python program using raise keyword that only accepts the word "PYTHON". If input is wrong, raise ValueError.
