-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday23.py
57 lines (44 loc) · 2.07 KB
/
day23.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Day 23: Simple Calculator
Create a simple calculator. The calculator should be able to perform basic math
operations, add, subtract, divide and multiply. The calculator should take input
from users. The calculator should be able to handle ZeroDivisionError, NameError, and
ValueError.
"""
# function to check if input is valid number ,it will give "True" in case of integer ,float and any decimal number ,
# we can use this function inside of calculator
def is_valid_number(value):
try:
float(value)
return True
except ValueError:
return False
def calculator():
# take input and choose operator
first_number = input("Please enter first number: ")
second_number = input("Please enter second number: ")
operator = input("To add, press 1. To subtract, press 2. To divide, press 3. To multiply, press 4.\n")
result = None # The issue might be that if the user selects the division operator and a ZeroDivisionError occurs,
#the except block prints an error message but does not return a value.
#This means that result is not defined in this case, causing an UnboundLocalError when the function attempts to return it.
# validate the input
if not is_valid_number(first_number) or not is_valid_number(second_number):
raise ValueError("Please enter valid number to continue!")
# perform calculation based on operator
if operator == "1":
if float(first_number) and float(second_number) > 0:
result = f'Result is {float(first_number) + float(second_number):.1f}'
else:
raise ValueError("Please work only with positive numbers!")
elif operator == "2":
result = float(first_number) - float(second_number)
elif operator == "3":
try:
result = f'Result is {float(first_number) / float(second_number):.1f}'
except ZeroDivisionError:
return "Error: Cannot divide by zero !"
else:
result = f'Result is {float(first_number) * float(second_number):.3f}'
return result
if __name__ == '__main__':
print(calculator())