forked from Sina-Saaber/simple-calculator-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-calculator.py
More file actions
126 lines (109 loc) · 4.22 KB
/
simple-calculator.py
File metadata and controls
126 lines (109 loc) · 4.22 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def select_operation():
print("Select a operation to perform >>>")
print("1. add")
print("2. subtract")
print("3. multiply")
print("4. divide")
print("5. power")
print("6. radical")
operation_select = input("Enter operation or its key number : ").lower()
print()
return operation_select
def convert_int_input(user_input):
while user_input not in ["", "exit"] :
try:
number = float(user_input)
return number
except ValueError:
print("Invalid input!! try a true number...")
user_input = input("Enter another number: ")
def add_operation():
result = 0
user_input = input("Enter Your number: ")
while user_input not in ["", "exit"] :
number = convert_int_input(user_input)
result += number
user_input = input("Enter another number: ")
return result
def subtract_operation():
user_input = 0
number = 0
first_input = input("Enter Your first number: ")
result = convert_int_input(first_input)
while user_input not in ["", "exit"] :
number = convert_int_input(user_input)
result -= number
user_input = input("Enter another number: ")
return result
def multiply_operation():
result = 1
user_input = input("Enter Your number: ")
while user_input not in ["", "exit"] :
number = convert_int_input(user_input)
result *= number
user_input = input("Enter another number: ")
return round(result, 3)
def divide_operation():
first_input = input("Enter Your first number: ")
user_input = 1
if first_input not in ["", "exit"] :
result = convert_int_input(first_input)
while user_input not in ["", "exit"] :
user_input = convert_int_input(input("Enter another number: "))
if user_input not in ["", "exit", None] :
try:
result /= user_input
except ZeroDivisionError as e:
print("Zero divison error!!",e , "\ntry again...")
else:
break
return round(result, 3)
# user_input = 0 # debug-defult
# first_input = input("Enter Your first number: ")
# result = convert_int_input(first_input)
# while user_input not in ["", "exit"] :
# number = convert_int_input(user_input)
# try:
# result /= number
# except ZeroDivisionError:
# print("Zero divison error!! try again...")
# user_input = input("Enter another number: ")
# return round(result, 3)
def power_operation():
result = 1
first_input = input("Inter your first number: ")
first_number = convert_int_input(first_input)
secened_input = input("Inter your secend number (power number): ")
power = int(convert_int_input(secened_input))
for i in range(0, power):
result *= first_number
return round(result, 3)
def radical_operation():
user_input = input("Enter Your number: ")
number = int(convert_int_input(user_input))
return round((number ** 0.5), 3)
# i = 0
# while i < number :
# if (number - i*i) < 0.1 :
# return round(i, 3)
# i += 0.001
# print("Radical is not defind !!")
# main function that do conecting and handeling to other functions
def main_luancher(operation_select):
print('Type and enter "exit" or just press <enter> for exit')
if operation_select in ["1", "add"] :
print(f"Result : {add_operation()}")
elif operation_select in ["2", "subtract"]:
print(f"Result : {subtract_operation()}")
elif operation_select in ["3", "mulitply"] :
print(f"Result : {multiply_operation()}")
elif operation_select in ["4", "divide"] :
print(f"Result : {divide_operation()}")
elif operation_select in ["5", "power"] :
print(f"Result : {power_operation()}")
elif operation_select in ["6", "radical"] :
print(f"Result : {radical_operation()}")
else:
print("Invalid input!! try again...")
main_luancher(select_operation())
main_luancher(select_operation())