Page 137 - CodePilot V5.0 C7
P. 137
Output
Enter Time in 24-hour format: 6 Write a Python program to
Good Morning enter a number. If it is less than
0, display: ‘Negative numbers
Enter Time in 24-hour format: 18
are not allowed’.
IF … ELSE … STATEMENT
The if…else… statement lets your program choose between two options. Python checks the
condition; if it’s true, it executes statement block 1 after the if statement—otherwise, it executes
statement block 2 after the else statement. Start
Syntax of the if … else … statement:
if (condition):
Input Age
Statement block 1
else:
False
Statement block 2 Is Age>18?
Note that both statement blocks 1 and 2 are
indented. Block 1 is under if, so it will be executed True Print ‘Not Eligible’
if the condition mentioned is True. Block 2 is under Print ‘Eligible’
else, so it will be executed if the condition is False.
Here, if the age of the user is more than 18, ‘Eligible’
will be printed, otherwise ‘Not Eligible’ will be printed. Stop
Program 3 To show a message if the user is ‘Eligible’ or ‘Not Eligible’ to cast a vote.
Program3.py
File Edit Format Run Options Window Help
age=int(input("Enter Age: "))
if ( age>=18):
print("You are eligible to cast a vote.")
else:
print("You are not eligible to cast a vote.")
Output
Enter Age: 25
You are eligible to cast a vote.
Enter Age: 15
You are not eligible to cast a vote.
135
Flow of Control in Python

