Chapter 6 - Nested if Statements and Logical Operators

example 6.1, page no. 125

In [1]:
print "Enter your age: ",
age = int(raw_input())
print "Are you a citizen (Y/N): ",
choice = raw_input()
if (choice == 'Y'):
    citizen = True
else:
    citizen = False
if (age >= 18):
    if citizen:
        print "You are eligible to vote"
    else:
        print "You are not eligible to vote"
else:
    print "You are not eligible to vote"
Enter your age: 65
 Are you a citizen (Y/N): Y
 You are eligible to vote

example 6.2, page no. 127

In [4]:
print "Enter your age: ",
age = int(raw_input())
if (age > 12):
    if (age >= 65):
        print "Admission is free"
    else:
        print "You have to pay"
else:
    print "Admission is free"
Enter your age: 11
 Admission is free

example 6.3, page no. 129

In [7]:
print "Enter your age: ",
age = int(raw_input())
print "Are you a citizen (Y/N): ",
choice = raw_input()
if (choice == 'Y'):
    citizen = True
else:
    citizen = False
if (age >= 18 and citizen == True):
    print "You are eligible to vote"
else:
    print "You are not eligible to vote"
 Enter your age: 22
 Are you a citizen (Y/N): N
 You are not eligible to vote

example 6.4, page no. 131

In [8]:
print "Enter your age: ",
age = int(raw_input())
if (age <= 12 or age >= 65):
    print "Admission is free"
else:
    print "You have to pay"
Enter your age: 22
 You have to pay

example 6.5, page no. 132

In [9]:
print "Enter your age: ",
age = int(raw_input())
if not ((age > 12 and age < 65)):
    print "Admission is free"
else:
    print "You have to pay"
Enter your age: 22
 You have to pay