Skip to content

Commit 430061d

Browse files
authored
Merge pull request #1 from joshs47/fix/sanitize
Fix/sanitize
2 parents b7b75a7 + a7207d7 commit 430061d

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

labs/lab_1/lab_1b.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,21 @@ def simple_calculator(operation: str, num1: float, num2: float) -> float:
3737
else:
3838
raise ValueError("Invalid operation. Please choose from 'add', 'subtract', 'multiply', or 'divide'.")
3939

40+
def request_sanitized_number(prompt: str) -> float:
41+
#make sure no error when user enters something other than number
42+
while True:
43+
try:
44+
number = float(input(prompt))
45+
return number
46+
except ValueError:
47+
print("Invalid input. Please enter a valid number.")
4048
def main():
4149

4250
print(f"===== Simple Calculator =====")
4351

4452
# Ask the user for sample input
45-
num1 = float(input("Enter the first number: "))
46-
num2 = float(input("Enter the second number: "))
53+
num1 = request_sanitized_number("Enter the first number: ")
54+
num2 = request_sanitized_number("Enter the second number: ")
4755
operation = input("Enter the operation (add, subtract, multiply, divide): ").strip().lower()
4856

4957
# Perform the calculation and display the result

0 commit comments

Comments
 (0)