+ 1
Transform string "+" or "*" into arithmetic operators
We can convert a string to a number: int("5") => 5 Can we transform string "+" or "*" into arithmetic operators?(without eval())
6 odpowiedzi
+ 2
You can do something like this:
from operator import add, mul
op = {'+': add, '*': mul}
print(op['+'](2, 3)) # 2+3=5
print(op['*'](2, 3)) # 2*3=6
https://code.sololearn.com/cBp52U0M2ysS/?ref=app
+ 3
Yes we can try this:
x=chr(ord("+"))
print(x)
>>> +
And,
x=chr(ord("*"))
print(x)
>>> *
+ 1
Tibor Santa, closest
+ 1
You may find answer in https://javascript.info/operators
0
Not directly I don't think. You can use if
if symbol == "+":
print(a+b)
Or exec
exec("print(a" + symbol + "b)")
0
L3ARN3R, this is the same