CONTENT
- Introduction
- Solved Problems
- Unsolved Problems
- Check Your Understanding
​Introduction
Coding Solved Problems
​1. Coffee Crisis Detector:
Write a Python program that defines a variable coffee_cups to represent the number of coffee cups available. Using a single if statement, check if the number of coffee cups is less than 1. If this condition is true, print a message warning that productivity levels are dropping and a coffee refill is urgently needed.
Output:
Alert: Productivity levels dropping—refill coffee immediately!
​2. Code Comment Confession:
Create a Python program with a variable lines_of_comments indicating the number of comment lines in a script. Using only one if statement, determine if the number of comments is fewer than 10. If so, print a message cautioning that insufficient documentation may cause future regret.
Output:
Warning: Future you will regret this lack of documentation!
​3. Pizza Motivation Check:
Develop a Python program that includes a variable hours_worked to track the number of hours worked. With a single if statement, evaluate whether the hours worked are greater than or equal to 8. If this condition holds, print a congratulatory message announcing that a pizza break has been earned.
Output:
Congratulations! You’ve earned a pizza break!
​4. Procrastination Detector:
Construct a Python program with a variable tasks_due representing the number of pending tasks. Using a single if statement, check if the number of tasks due exceeds 0. If true, print a robotic-sounding message urging the user to stop procrastinating and begin working.
Output:
Stop scrolling and start working!
​5. Procrastination Detector:
Write a Python program that defines a variable temperature to store the current temperature in degrees. Using only one if statement, assess whether the temperature exceeds 25 degrees. If it does, print a instruction stating that shorts and t-shirts are now required.
Output:
Caution: Shorts and t-shirts are required!
Coding Unsolved Problem
Minimum Balance Checker
Create a Python program with a variable account_balance representing a bank account’s current balance in rupees. Using only one if statement, determine if the balance is below 100. If so, print a notification advising the account holder to deposit funds.
Speed Limit Warning
Develop a Python program that includes a variable speed representing a vehicle’s current speed in kilometers per hour. Using a single if statement, evaluate whether the speed exceeds 120 km/h. If this condition is true, print a warning about exceeding the speed limit.
Login Attempt Monitor
Write a Python program that defines a variable login_attempts to track the number of failed login attempts. Using a single if statement, determine if the number of attempts exceeds 3. If this condition holds, print a security alert indicating a potential unauthorized access attempt.
Concept Problems
1. Explain the basic syntax and functionality of a simple 'if' statement?
2. What types of data can be used in the condition part of an 'if' statement?
- Boolean variables or literals directly (e.g., if True:).
- Comparison operators (e.g., if x > 5:, if y == 10:).
- Functions or expressions that return a boolean value.
- Falsy values: False, None, 0, 0.0, 0j, '', (), [], {}. Any object whose __bool__() method returns False or whose __len__() method returns zero.
- Truthy values: Any value that is not falsy.
- Python evaluates the condition to a boolean internally.
3. Provide an example of a situation where you would use a simple 'if' statement without an 'else' or 'elif' clause in Python?
- Boolean variables or literals directly (e.g., if True:).
- Comparison operators (e.g., if x > 5:, if y == 10:).
- Functions or expressions that return a boolean value.
- Falsy values: False, None, 0, 0.0, 0j, '', (), [], {}. Any object whose __bool__() method returns False or whose __len__() method returns zero.
- Truthy values: Any value that is not falsy.
- Python evaluates the condition to a boolean internally.
