Sunday, April 25, 2021

Python 3 - Nested Control Statement

Learning nested control statement in Python 3.
Taking notes on what I missed during the learning process.

1.  Remember to add comma when displaying str and variable together.
     Example: print ('Passed: ',(pass_counter))
2.  Learn to use nested control such as if...else within the for loop.
3.  Use for statement and the range function is appropriate when we know in advance that there are 10 exam results.

# fig03_03.py
"""Using nested control statements to analyze examination results."""

# initialize variables
pass_counter = 0 # number of passes
fail_counter = 0 # number of failures

# process 10 students
# exam_grades = int(input('Enter 1 for pass or 2 for fail: '))
for student in range(10):
    # get one exam result
    result = int(input('Enter result (1=pass, 2=fail): '))
    
    if result == 1:
        pass_counter += 1
    else:
        fail_counter += 1

# termination phase
print('Passed: ',(pass_counter))
print('Failed: ',(fail_counter))
    
if pass_counter > 8:
    print ('Bonus to instructor')