Question 1: Temperature Validation
Code:
def GetTemperature():
while True:
temp = float(input("Enter temperature (0.0 to 100.0): "))
if temp < 0.0 or temp > 100.0:
print("Invalid temperature!")
continue
print("Temperature recorded.")
break
return temp
current_temp = GetTemperature()
print(f"Final temperature: {current_temp}")
Modification Tasks:
Change the valid range from 0.0–100.0 to 34.0–42.0.
Modify the error message to display: "Out of safe range! Re-entry required.".
Add a print statement before the input to display "Scanning body temperature..."
every time the loop runs.
Modify the function to include a counter that tracks how many times an invalid
temperature was entered and prints the total once a valid one is accepted.
Question 2: Stock Quantity Entry
Code:
def GetStockQuantity():
while True:
quantity = int(input("Enter quantity to order (1 to 50): "))
if quantity < 1 or quantity > 50:
print("\nError - try again")
continue
print("Order confirmed.")
break
return quantity
total_order = GetStockQuantity()
print(f"Total items ordered: {total_order}")
Modification Tasks:
Change the valid range from 1–50 to 10–100.
Modify the function so it only accepts multiples of 5 (e.g., 10, 15, 20) within the range.
Modify the error message to: "Invalid amount! Orders must be in batches of 5.".
Add a print statement before asking for input to display "Checking warehouse
availability..." every time the loop runs.
Question 3: Security Code Validation
Code:
def GetSecurityCode():
while True:
code = input("Enter a 4-digit security code: ")
if len(code) != 4:
print("\nInvalid length!")
continue
print("Code verified.")
break
return code
# Testing
valid_code = GetSecurityCode()
print(f"Access granted with code: {valid_code}")
Modification Tasks:
Change the required length from 4 characters to 6 characters.
Modify the error message to: "Security breach! Code must be exactly 6 characters.".
Modify the function so that it only accepts codes containing only alphabetic letters (no
numbers).
Add a counter that keeps track of invalid attempts. If the user fails 3 times, the function
should break and return the message "Account Locked".
Question 4: Math Quiz Game
Code:
import random
def MathQuiz():
num1 = [Link](1, 10)
num2 = [Link](1, 10)
correct_answer = num1 + num2
attempts = 0
while True:
user_answer = int(input(f"What is {num1} + {num2}? "))
attempts += 1
if user_answer == correct_answer:
print(f"Correct! It took you {attempts} tries.")
break
else:
print("Wrong answer! Try again.")
MathQuiz()
Modification Tasks:
Change the range of the random numbers from 1–10 to 10–50.
Modify the game so that the player has only 4 attempts before the game ends with a
"Game Over" message.
Add a print statement before the input that says "Mental Math Challenge!" every time
the loop runs.
Modify the validation logic so the function only accepts even numbers as valid inputs; if
an odd number is entered, display "Error: Please enter an even number" and do not
count it as a math attempt.