Introduction to Conditional Statements
Overtime pay requires applying a 1.5x multiplier only to hours worked above 40; the calculation path diverges at this threshold.
When the Path Changes
A conditional statement lets a program choose a path based on an input or situation. The important idea is not just writing an if statement; it is designing the program so that each possible input reaches the correct outcome. Overtime pay, invalid user input, and grade assignment all require the program to make a decision and then execute only the appropriate branch.
Overtime at the Threshold
The overtime problem has a clear decision point at 40 hours. When the number of hours is 40 or less, the regular-pay path is used. When the number is greater than 40, only the hours above 40 receive the 1.5x multiplier. The first 40 hours remain on the regular-pay path.
Tracing 45 hours
Assume a worker earns a regular rate of 20 per hour and works 45 hours. Apply the overtime rule that hours above 40 receive a 1.5x multiplier.
Check the threshold: 45 is greater than 40, so the overtime branch is selected.
Separate the hours: The regular portion is 40 hours. The overtime portion is 45 minus 40, which is 5 hours.
Apply the multiplier: The 5 overtime hours use the regular rate multiplied by 1.5. The first 40 hours use the regular rate.
The calculation is 40 times 20 plus 5 times 20 times 1.5, giving 950.
hours = 45 rate = 20 if hours > 40: regular_hours = 40 overtime_hours = hours - 40 pay = regular_hours * rate + overtime_hours * rate * 1.5 else: pay = hours * rate print(pay)
Protecting Input Conversion
User input may not be numeric when the program expects a number. A try block can contain the conversion and processing steps, while an except block provides a graceful response when the conversion cannot handle the supplied input. This keeps the program from simply failing at the point where the unexpected input appears.
Validating Before Processing
Range validation adds another decision after input has been converted. The program first determines whether the value is within the accepted range. Valid input continues to the conditional processing, while invalid input receives an invalid-input response. This prevents values outside the intended range from being treated as normal cases.
For a score represented between 0.0 and 1.0, values such as 1.5 and -0.1 should be treated as out of range. Boundary values deserve special attention because they test whether comparisons include or exclude the exact threshold.
Chaining Grade Decisions
A multi-branch conditional checks several possible ranges in sequence. The illustrative grading logic below first rejects scores outside 0.0 through 1.0. It then checks the highest threshold first and continues through lower thresholds until one matches. The final else branch handles the remaining valid scores.
For a score of 0.85, the output is B. For a score of 1.5, the output is Invalid score. For text that cannot be converted to a number, the except branch prints Please enter a numeric score.Tracing a score of 0.85
Use the illustrative grade thresholds to determine the result for a score of 0.85.
Validate the range: 0.85 is between 0.0 and 1.0, so processing continues.
Check the first threshold: 0.85 is below 0.9, so the A branch is skipped.
Check the next threshold: 0.85 is at least 0.8, so the B branch is selected.
Stop at the matching branch: The later branches are not needed after the matching branch is reached.
The illustrative grade is B.
Testing Every Boundary
Conditional logic should be tested with normal inputs, boundary values, edge cases, and intentionally bad input. For overtime, test fewer than 40 hours, exactly 40 hours, and more than 40 hours. For exception handling, test numeric input, text, negative values, and very large values. For grading, test the threshold values 0.9, 0.8, 0.7, and 0.6, values between them, and out-of-range values such as 1.5 and -0.1.
Applying the 1.5x multiplier to all hours
The multiplier applies only to hours above 40, not to the first 40 hours.
Fix:
Separate regular hours from overtime hours before applying the multiplier.Testing only ordinary values
A conditional can appear correct while mishandling the threshold itself.
Fix:
Test values below the threshold, exactly at the threshold, and above it.Treating non-numeric input as a valid number
Text supplied where a number is expected may prevent the intended processing path.
Fix:
Place the conversion in try and provide a graceful response in except.Skipping range validation
An input can be numeric but still outside the accepted range.
Fix:
Validate the range before running the grade thresholds.Debugging the result without checking the branch
The error may be that a different conditional path was selected.
Fix:
Print the input and log which branch was taken immediately after each condition.
Practice the Paths
Write a program that asks for hours worked and a regular hourly rate. If the hours are greater than 40, calculate regular pay for the first 40 hours and apply the 1.5x multiplier only to the remaining hours. Otherwise, calculate pay using the regular rate. Test the program with fewer than 40 hours, exactly 40 hours, more than 40 hours, text input, a negative number, and a very large number.
Hints
- Start by checking whether the hours value is greater than 40.
- When the overtime branch is selected, calculate overtime hours by subtracting 40 from the total hours.
- Use try and except around numeric conversion so non-numeric input receives a graceful response.
- Record which branch executes during testing.
Write a second program that accepts a score from 0.0 through 1.0. Reject values outside that range, handle non-numeric input with try and except, and use the thresholds 0.9, 0.8, 0.7, and 0.6 to assign the illustrative grades A, B, C, D, or F. Test every threshold exactly, values between thresholds, 1.5, -0.1, and text.
Hints
- Perform range validation before the grade comparisons.
- Check the highest threshold first.
- Use an else branch for the remaining valid scores.
- Trace the score and the branch selected when a result is unexpected.
Conditional Logic Checklist
- A conditional statement routes an input to a particular processing path.
- For overtime pay, the 40-hour threshold separates regular processing from overtime processing, and only hours above 40 receive the 1.5x multiplier.
- A try and except structure lets a program respond gracefully when input cannot be converted to a number.
- Range validation should happen before further conditional processing when only a specific input range is valid.
- Boundary values, edge cases, and intentionally bad input reveal whether every branch works as intended.
Key Takeaways
- Conditional logic determines which block of code executes for a particular input.
- Overtime calculations diverge at 40 hours, with the 1.5x multiplier applied only to hours above that threshold.
- Try and except blocks provide a graceful path for non-numeric input.
- Range validation and ordered multi-branch checks route valid values to the correct outcome.
- Testing thresholds, edge cases, and bad input is essential for reliable conditional programs.