Hi, Why the condition "if" in my code does not work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Hi, Why the condition "if" in my code does not work?

I want using "index()", but how should it be? https://code.sololearn.com/cYP9M04qF8Zg/?ref=app

28th Jul 2021, 7:22 AM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
7 Answers
+ 14
Egor Tonchev(EGO) If condition works there ๐Ÿคฃ But there is a little problem with your code If u print your list before for then you will get the problem causing part.๐Ÿ’ฏ this is printed when you print that list [4*3, 12-3 ,3+6 , 40/20'] BUT you need this [ 12,9,9,2] Soi you can solve it by using eval function ๐Ÿ‘ or some other method
28th Jul 2021, 7:34 AM
Sฯ‰Ati
Sฯ‰Ati - avatar
+ 6
With the help of all of You I've solved the problem. Special Thanks to Carlos, Swati ๐Ÿ”น and Calvin Thomas ๐Ÿ™‚๐Ÿ‘
11th Aug 2021, 2:08 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
12th Aug 2021, 5:00 PM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
+ 2
'none' is always appeared in tasting.
28th Jul 2021, 8:45 AM
Egor Tonchev(EGO)
Egor Tonchev(EGO) - avatar
0
By using eval: for j in mathact: if mathres==eval(j): print("index"+" " +str(mathact.index(j))) break elif j==mathact[-1]: print('none')
28th Jul 2021, 7:51 AM
Carlos
Carlos - avatar
0
Egor Tonchev(EGO) You haven't configured the second input: Use: mathact = input().split()
28th Jul 2021, 9:01 AM
Carlos
Carlos - avatar
0
Here's a possible solution: num = int(input()) arr = input().split() i = 0 while eval(arr[i]) != num: i += 1 print("index", i) # Hope this helps
28th Jul 2021, 9:29 AM
Calvin Thomas
Calvin Thomas - avatar