+ 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)
10 odpowiedzi
+ 6
while m[:-4:-1][::-1] not in (user, cpu):
m.append(randint(0, 1))
wins += m[:-4:-1][::-1] == cpu
+ 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
+ 2
yes you can, using and
while condA and condB
+ 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
+ 2
Mert Yazici That worked!! Thanks a lot
+ 2
Anna That works! How did this not occur to me! Anyway thanks a lot!!
+ 1
It doesn't change a whole lot, but could you connect your two ifs with or? Then you have only one break case...
+ 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😊👍
0
but that doesn't work for me as I need to use or.
Even then, that syntax doesn't work
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.