简体   繁体   中英

While loop if condition is not met

I want to break out of the loop if y or n is set. In my head this is the way to do it, but it seems to get stuck in the while loop even when 'str1' is set to both n and y.

I have also tried to do: while str1:= "y" or str1 != "n": without luck.

str1 = ""
while not str1 == "y" or not str1 == "n": 
    str1 = input('setting [y/n] => ')
    str1 = str1.lower()

Your condition while str1 != "y" or str1 != "n" is always True , if you enter n it'll be different of y and vice-versa.

You want to stop if both condition aren't met

while str1 != "y" and str1 != "n"

Or easier

while str1 not in "yn":

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM