There seems to be a syntax error in the "
elif choice == "%":
" statement. Please help me out.
What I have tried:
1 def add(a, b):
2 return a + b
3
4
5 def subtract(a, b):
6 return a - b
7
8
9 def multiply(a, b):
10 return a * b
11
12
13 def divide(a, b):
14 return a / b
15
16
17 def power(a, b):
18 return a ^ b
19
20
21 def remainder(a, b):
22 return a % b
23
24
25
26
27
28 def select_op(choice):
29 if (choice == "#"):
30 return -1
31
32 elif (choice in('+','-','*','/','^','%','#','$')):
33 while true:
34 firstnum=input("Enter the first number:")
35 print(firstnum)
36 try:
37 if firstnum == '0$':
38 return 0
39 else:
40 num1=float(firstnum)
41 except:
42 print("Not a valid number,please enter again")
43 continue
44 secondnum=input("Enter the second number: ")
45 print(secondnum)
46 try:
47 if secondnum=='0$':
48 return 0
49 elif secondnum=='#':
50 return -1
51 else:
52 num2=float(secondnum)
53 break
54 except:
55 print("Not a valid number,please enter again")
56 continue
57 if choice =="+":
58
59 print(num1,"+", num2,"=",add(num1,num2))
60
61 elif choice == '-':
62 print(num1,"-", num2,"=",subtract(num1,num2))
63
64 elif choice == '*':
65 print(num1,"*", num2,"=",multiply(num1,num2))
66
67 elif choice == '/':
68 if num2==0:
69 print("float division by zero")
70 print(num1,"/",num2,"=", divide(num1,num2))
71
72 elif choice == '^':
73 num1=int(num1)
74 num2=int(num2)
75 print(num1,"^", num2,"=",power(num1,num2))
76
77
78 elif choice == "%":
79 print(num1,"%", num2,"=",remainder(num1,num2))
80 elif choice == '$':
81 return select_op()
82 else:
83 print("Unrecognized operation")
84
85
86
87
88
89 while True:
90 print("Select operation.")
91 print("1.Add : + ")
92 print("2.Subtract : - ")
93 print("3.Multiply : * ")
94 print("4.Divide : / ")
95 print("5.Power : ^ ")
96 print("6.Remainder: % ")
97 print("7.Terminate: # ")
98 print("8.Reset : $ ")
99
100
101
102 choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
103 print(choice)
104 if(select_op(choice) == -1):
105
106 print("Done. Terminating")
107 exit()