Why this is not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why this is not working?

def m(x,y): return x*y def p(x,y): return x+y def r(x,y): return x-y def impr(ordr, x, y): return ordr(x,y) n1=int(input("n1 :")) n2=int(input("n2 :")) entr=str(input("que? ")) print(impr(entr, n1, n2)) print(impr(m, 3, 3)) print(impr(p, 3, 3)) print(impr(r, 3, 3)) I can't understand why that code is giving me an error, but when I delete this part: n1=int(input("n1 :")) n2=int(input("n2 :")) entr=str(input("que? ")) print(impr(entr, n1, n2)) It's working fine. Can any one help me plz?

20th Apr 2019, 10:08 AM
Vadym R.
Vadym R. - avatar
3 Answers
+ 5
Ofcourse. It results in an error because you are trying to call a string. Try to change the line: entr=str(input("que? ")) To: entr=eval(str(input("que? "))) And because the input function always returns a string, you can remove the str function: entr=eval(input("que? "))
20th Apr 2019, 10:19 AM
Seb TheS
Seb TheS - avatar
+ 3
Some people anyways discourage the use of eval function, you can fix this with a dictionary: d = {"m": m, "p": p, "r": r} And change the line: entr=str(input("que? ")) To: entr=d[input("que? ")]
20th Apr 2019, 10:24 AM
Seb TheS
Seb TheS - avatar
+ 1
Thanks a lot Seb TheS !!!!
20th Apr 2019, 10:21 AM
Vadym R.
Vadym R. - avatar