I have in the following code I have a function that performs certain operation according to the passed parameters. Then I have a dictionary serving as a switch case, which will serve to define which parameters will be passed to the function.
def calculate(number, operator):
for i in range(1, 11): print(eval(number + operator + str(i)))
inputs = list(input().split())
switch = {
'1': calculate(inputs[0], '+'),
'2': calculate(inputs[0], '*'),
'3': calculate(inputs[0], '/'),
'4': calculate(inputs[0], '-'),
}
switch[inputs[1]]
But when you run the program and choose the entries, the program executes all operations within the dictionary, not just the one selected by the user. For example, if the inputs are 5 and 1, instead of just doing the addition operation, we have the following output:
6
7
8
9
10
11
12
13
14
15
5
10
15
20
25
30
35
40
45
50
5.0
2.5
1.6666666666666667
1.25
1.0
0.8333333333333334
0.7142857142857143
0.625
0.5555555555555556
0.5
4
3
2
1
0
-1
-2
-3
-4
-5
Could anyone explain why and how to fix the problem?