0

Currently working on a project but have run a bit into a stand still with my program.

while(True):
    if p1.play() == False:
        break
    betInput = int(input("How much do you want to bet?"))
    while(betInput > p1.wallet or betInput <=0):
        betInput = int(input("How much do you want to bet?"))
        continue
    p1.bet(betInput)
    print("")

    for i in range(2):
        p1.getCard ( d.dealCard() )
        p2.getCard ( d.dealCard() )
    p1.show()
    p2.dealShow()
    gameInput = input("Hit or Stand? H\S")
    gameInput = gameInput.lower()
    p1.gameMove(gameInput)
    if p1.winCheck(betInput) == 1:
        continue

    while gameInput == 'h' and p1.cardCheck() != True or gameInput == 'hit' and p1.cardCheck() != True:
        gameInput = input("Hit or Stand? H\S")
        gameInput = gameInput.lower()
        p1.gameMove(gameInput)
        if p1.winCheck(betInput) == 1:
            if p1.play() == False:
                pass
        p2.winCheck(betInput)

The issue is that when I go to break out of "gameInput" loop (to quit the program), it just breaks out of it and then continues on down the code, is there a way I can have it loop back up to the first while loop? Or, is there a way to just terminate the program right then and there?

mossy
  • 61
  • 7
  • 1
    `break` can only exit the immediate loop. You can (ab)use exceptions to simulate the limited form of `goto` that you are looking for. – chepner Nov 14 '19 at 20:55
  • Create a boolean variable, and initialize it to False. In the outer loop, have an if condition, that breaks out of the loop if the variable is set to True. In the inner loop, if your condition is met, set the condition to True, break out of the inner loop, and then you will enter the condition for if the variable is True, and break out of the outer loop. – Matthew Kligerman Nov 14 '19 at 20:56
  • 1
    You problem suggests a design issue. Make your loop part of a function and have the caller decide what to do. – postoronnim Nov 14 '19 at 20:57

2 Answers2

0

If you wrap the code in a method, you can always return from any point, regardless of how nested it is.

Make sure, however, that all possible code paths return something.

Rui Vieira
  • 5,253
  • 5
  • 42
  • 55
0

If I understand your question correctly, you want to get back to the beginning of the while function. The continue function could serve you for this purpose.Continue function example

chyfil
  • 1
  • 1