Objectives
- Solve problems using Python selection
- Use Python if statements
- Use Python if/else statements
- Use Python nested if/else statements
- Validate user input (very basic validation)
if statement - used for single-branched selection
if test:
statement(s) # executed if test is true
if/else - used for two-branched selection
if test:
statement(s) # executed if test is true
else:
statement(s) # executed if test is false
nested if/else - used for complex selection
if test1:
statement(s) # executed if test1 is true
if test2:
statement(s) # executed if test1 and test2 are both true
else:
statement(s) # executed if test1 is true and test2 is false
else:
statement(s) # executed if test1 is false
chained conditionals
if test1:
statement(s) # executed if test1 is true
elif test2:
statement(s) # executed if test1 is false and test2 is true
else:
statement(s) # executed if test1 and test2 are both false
if eligibleForOvertime: if hoursWorked > 40: grossPay = hoursWorked * rate + (hoursWorked-40) * rate/2 else: grossPay = hoursWorked * rate