Home > Net >  cannot break out of while loop
cannot break out of while loop

Time:02-03

Trying to do a while loop, but it repeats even after I try to break it:

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. Hope someone can help me. Thanks in advance!

CodePudding user response:

Integer not equal string, that's why else statement print invalid input and while loop continue again. 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

CodePudding user response:

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".

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:

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.

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").

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.

CodePudding user response:

You are taking an integer input but in if-condition you are comparing it with a string value.

CodePudding user response:

I figured it out: This code worked in Jupyter Lab and didn't loop. But in Spyder 4.1.4 it did for some reason. Still unsre why. But hey, got it now. Thanks for the input

CodePudding user response:

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

  •  Tags:  
  • Related