'cannot break out of while loop

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!



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 preritdas
Solution 2 Anzar
Solution 3
Solution 4 Pr3daton