This repository was archived by the owner on Jun 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_Cacli.py
More file actions
66 lines (53 loc) · 1.66 KB
/
Copy pathBasic_Cacli.py
File metadata and controls
66 lines (53 loc) · 1.66 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
'''
Functionality
. Program asks the user to type
the first number.
· Program asks the user to type
a mathematical operator (a
choice of "
. Program asks the user to type
the second number.
· Program works out the result
based on the chosen
mathematical operator.
. Program asks if the user wants to continue working with the previous result.
If yes, program loops to use the previous result as the first number and then repeats the calculation process.
If no, program asks the user for the fist number again and wipes all memory of previous calculations.
Add the logo from art.py
'''
# TODO - dmake a function similarly of subtract , div and mult
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def divide(n1, n2):
return n1 / n2
def multiply(n1, n2):
return n1 * n2
# TODO - now add these into a dictionary as values whith keys as +,-,/,*
# what i learnt
dic = {
'+':add,
'-':subtract,
'/':divide,
'*':multiply
}
# TODO - now do 4*2 only using dic functions
print(dic['*'](4,2)) # dic['*'](4,2) --> multiply(4,2)
def calculator():
over = False
input1 = int(input("Enter 1st number: "))
while not over:
for i in dic:
print(i)
operator = str(input("Enter operator: "))
input2 = int(input("Enter 2nd number: "))
answer = (dic[operator](input1,input2))
print(f'{input1} {operator} {input2} = {answer}') #made a mistake here that i kept putting previous we dont need previous here
yn = input("Continue ? Enter y for yes n for no: ")
if yn == 'y':
input1 = answer
else:
print('Bye.')
over = True
calculator()