简体   繁体   English

无法跳出while循环

[英]cannot break out of while loop

Trying to do a while loop, but it repeats even after I try to break it:试图做一个while循环,但即使在我尝试打破它之后它也会重复:

while True:        
    
    switch = int(input("Choose card to switch: "))
    if switch == "1":
             deck1.add_card(hand1.pop_card())
             deck1.shuffle()
             hand1.add_card(deck1.pop_card())
             break

    elif switch == "2":
               deck1.add_card(hand2.pop_card())
               deck1.shuffle()
               hand2.add_card(deck1.pop_card())
               break

    else:
              print("invalid input")
              continue

The code between "if" and "break" works fine, but I just go back to being asked to choose a card. “if”和“break”之间的代码工作正常,但我只是 go 回到被要求选择一张卡。 Hope someone can help me.希望可以有人帮帮我。 Thanks in advance!提前致谢!

Integer not equal string, that's why else statement print invalid input and while loop continue again. Integer 不等于字符串,这就是 else 语句打印无效输入和 while 循环再次继续的原因。 Code:代码:

while True:        
switch = int(input("Choose card to switch: "))
if switch == 1:
         deck1.add_card(hand1.pop_card())
         deck1.shuffle()
         hand1.add_card(deck1.pop_card())
         break

elif switch == 2:
           deck1.add_card(hand2.pop_card())
           deck1.shuffle()
           hand2.add_card(deck1.pop_card())
           break

else:
          print("invalid input")
          continue

The most likely answer is that you're receiving integers from the user but conditionally checking against strings.最可能的答案是您从用户那里接收整数,但有条件地检查字符串。 Change the if conditions to be 1 and 2 instead of "1" and "2" .if条件更改为12而不是"1""2"

In general, if you're working with complicated structures of while loops and if clauses, this might be a better way to achieve what you're after:一般来说,如果您正在使用复杂的while循环和if子句结构,这可能是实现您所追求的更好的方法:

repeat = True
while repeat:
    switch = int(input("Choose card to switch: "))
    if switch == "1":
        deck1.add_card(hand1.pop_card())
        deck1.shuffle()
        hand1.add_card(deck1.pop_card())
        repeat = False
    elif switch == "2":
        deck1.add_card(hand2.pop_card())
        deck1.shuffle()
        hand2.add_card(deck1.pop_card())
        repeat = False
    else:
        print("invalid input")

While maybe not the most Pythonic approach, this is a good way to trace the logic of complex conditional structures.虽然可能不是最 Pythonic 的方法,但这是追踪复杂条件结构逻辑的好方法。

Again, your problem is most likely just that you're receiving integers from the user ( int(input("Choose card to switch: ")) ) and checking against strings ( if switch == "1" ).同样,您的问题很可能只是您从用户那里接收整数( int(input("Choose card to switch: ")) )并检查字符串( if switch == "1" )。

You should first try changing this like so:您应该首先尝试像这样更改:

while True:
    switch = int(input("Choose card to switch: "))
    if switch == 1:
        deck1.add_card(hand1.pop_card())
        deck1.shuffle()
        hand1.add_card(deck1.pop_card())
    elif switch == 2:
        deck1.add_card(hand2.pop_card())
        deck1.shuffle()
        hand2.add_card(deck1.pop_card())
    else:
        print("invalid input")

Try that before using repeat = True and while repeat .在使用repeat = Truewhile repeat之前尝试一下。

You are taking an integer input but in if-condition you are comparing it with a string value.您正在使用 integer 输入,但在 if 条件下,您将其与字符串值进行比较。

I figured it out: This code worked in Jupyter Lab and didn't loop.我想通了:这段代码在 Jupyter Lab 中工作并且没有循环。 But in Spyder 4.1.4 it did for some reason.但在 Spyder 4.1.4 中,出于某种原因它确实如此。 Still unsre why.仍然不确定为什么。 But hey, got it now.但是,嘿,现在明白了。 Thanks for the input感谢您的输入

it's because you are receiving the input as an integer and in the if statement you are checking if the type is string and equal to 1 so you must either change the if statement condition to be integer or to receive the input as string这是因为您将输入作为 integer 接收,并且在 if 语句中您正在检查类型是否为字符串并且等于 1,因此您必须将 if 语句条件更改为 integer 或将输入作为字符串接收

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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