Is there any way to pass in multiple conditions for a while loop without using a nested if statement? (see lines 31 to 36) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there any way to pass in multiple conditions for a while loop without using a nested if statement? (see lines 31 to 36)

https://code.sololearn.com/cj3OMBOg93Ne/?ref=app

20th Oct 2018, 1:26 PM
Varun Shah
Varun Shah - avatar
10 Answers
+ 6
while m[:-4:-1][::-1] not in (user, cpu): m.append(randint(0, 1)) wins += m[:-4:-1][::-1] == cpu
20th Oct 2018, 8:12 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 3
Maybe while True: if m[:-4:-1][::-1] in [user, cpu]: wins += m[:-4:-1][::-1] == cpu break m.append(randint(0, 1)) I can't test it because code playground seems to be down again
20th Oct 2018, 3:49 PM
Anna
Anna - avatar
+ 2
yes you can, using and while condA and condB
20th Oct 2018, 2:24 PM
Taste
Taste - avatar
+ 2
correct me if im wrong but you need to break the loop if the condition are true right ? did you change == to != ? while m[:-4:-1][::-1] != user or m[:-4:-1][::-1] != cpu: if m[:-4:-1][::-1] == cpu: wins += 1 break
20th Oct 2018, 3:22 PM
Taste
Taste - avatar
+ 2
Mert Yazici That worked!! Thanks a lot
21st Oct 2018, 12:40 AM
Varun Shah
Varun Shah - avatar
+ 2
Anna That works! How did this not occur to me! Anyway thanks a lot!!
21st Oct 2018, 12:44 AM
Varun Shah
Varun Shah - avatar
+ 1
It doesn't change a whole lot, but could you connect your two ifs with or? Then you have only one break case...
20th Oct 2018, 3:02 PM
HonFu
HonFu - avatar
+ 1
HonFu That is what I did earlier. But I changed it just to increment the wins counter in lesser number of lines. Thanks for the input though😊👍
20th Oct 2018, 3:04 PM
Varun Shah
Varun Shah - avatar
0
but that doesn't work for me as I need to use or. Even then, that syntax doesn't work
20th Oct 2018, 2:50 PM
Varun Shah
Varun Shah - avatar
0
Taste You are right. For the conditions on the while loop, I do have to use !=. But for me two. conditions in the while loop makes it an infinite loop. that never matches a condition.
21st Oct 2018, 12:43 AM
Varun Shah
Varun Shah - avatar