Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions labs/lab_1/lab_1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float:
raise ValueError("Cannot divide by zero.")
else:
raise ValueError("Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.")
while True:
try:
number = float(input(prompt))
return number
except ValueError:
print("Invalid input. Please enter a valid number.")
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operation = input("Enter the operation (add, subtract, multiply, divide): ").strip().lower()
result = simple_calculator(operation, num1, num2)
print(f"The result of {operation}ing {num1} and {num2} is: {result}")
break
except ValueError as e:
print(e)
print("Please try again.")

def main():

Expand Down