+ 1
Help
How can I get convert a user input from a string to a symbol such as / * %+- without specifying every symbol
2 Respostas
+ 12
As evil as it is, you have eval()
https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/built-in/eval
E.g.
a = "5"
b = "39"
c = input()
print(eval(a+c+b))
Let's say you input "+", the output would be 44.
+ 11
Based on: https://stackoverflow.com/questions/1740726/turn-string-into-operator
a = 5
b = 39
c = input()
ops = {
"+": (lambda x,y: x+y),
"-": (lambda x,y: x-y),
"/": (lambda x,y: x/y),
"*": (lambda x,y: x*y)
}
print(ops[c](a,b))