Hey I’m trying to make a small calculator for a project, however I’m struggling to get the code to work.
JavaScript
x
42
42
1
import time
2
3
a = 100
4
b = 1
5
6
while a > b:
7
print("Please select an operation. ")
8
print("1. Multiply")
9
print("2. Divide")
10
print("3. Subtract")
11
print("4. Add")
12
b = 200
13
x = int(input("Please enter your operation number. "))
14
if x == 4:
15
y = int(input("Please enter your first number. "))
16
z = int(input("Please enter your second number. "))
17
finaladd = (y+z)
18
print(finaladd)
19
c = input("Would you like to do another calculation? ")
20
if c == 'yes' or c == 'Yes':
21
a = 500
22
if c == 'no' or 'No':
23
b = 1000
24
25
if x == 2:
26
y2 = int(input("Please enter your first number. "))
27
z2 = int(input("Please enter your second number. "))
28
finaladd2 = (y2/z2)
29
print(finaladd2)
30
31
if x == 3:
32
y3 = int(input("Please enter your first number. "))
33
z3 = int(input("Please enter your second number. "))
34
finaladd3 = (y3-z3)
35
print(finaladd3)
36
37
if x == 1:
38
y4 = int(input("Please enter your first number. "))
39
z4 = int(input("Please enter your second number. "))
40
finaladd4 = (y4*z4)
41
print(finaladd4)
42
When I try to run operation 4, I want the code to ask if you would like to run another calculation, if you answer with ‘yes’ it will repeat the statement again. However all it does is stop the code. Sorry if it’s obvious I’m quite new to coding.
Advertisement
Answer
Try this…
JavaScript
1
42
42
1
import time
2
3
a = 100
4
b = 1
5
6
while a > b:
7
print("Please select an operation. ")
8
print("1. Multiply")
9
print("2. Divide")
10
print("3. Subtract")
11
print("4. Add")
12
b = 200
13
x = int(input("Please enter your operation number. "))
14
if x == 4:
15
y = int(input("Please enter your first number. "))
16
z = int(input("Please enter your second number. "))
17
finaladd = (y+z)
18
print(finaladd)
19
c = input("Would you like to do another calculation? ")
20
if c == 'yes' or c == 'Yes':
21
a = 500
22
elif c == 'no' or c == 'No':
23
b = 1000
24
25
elif x == 2:
26
y2 = int(input("Please enter your first number. "))
27
z2 = int(input("Please enter your second number. "))
28
finaladd2 = (y2/z2)
29
print(finaladd2)
30
31
elif x == 3:
32
y3 = int(input("Please enter your first number. "))
33
z3 = int(input("Please enter your second number. "))
34
finaladd3 = (y3-z3)
35
print(finaladd3)
36
37
elif x == 1:
38
y4 = int(input("Please enter your first number. "))
39
z4 = int(input("Please enter your second number. "))
40
finaladd4 = (y4*z4)
41
print(finaladd4)
42