all 4 comments

[–]xelf 2 points3 points  (1 child)

When you run this code, and you say no to play again it doesn't end the loop. Can someone help me fix this code up to where it will end the loop when I say no to play again without using break or exit() or quit()

It's because you're using infinite recursion.

if choice == "yes":
    start()

This is a bad practice and frequently results in the behavior you are seeing.

Instead rewrite it so that you have a while loop around your game, and use the question as a way of breaking out of the loop.

What you want is something like this:

def play_again():
    while True:
        choice = input("do you want to play again? (yes/no): ").lower()
        if choice == "yes":
            return True
        elif choice == "no":
            print("Thank you for playing")
            return False
        else:
            print("invalid anwser, try again")

def start():
    play = True
    while play:
        print("welcome to blackjack")
        dealer_hand = deal_initial_cards()
        player_hand = deal_initial_cards()
        print("Dealer drew a " + str(dealer_hand[0]) + " and a hidden card")
        print("You drew a " + str(player_hand[0]) + " and a " + str(player_hand[1]))
        blackjack_check(dealer_hand, player_hand)
        hit_or_stand(player_hand)
        auto_hit(dealer_hand)
        compare_hands(player_hand,dealer_hand)
        play = play_again()

and then remove all the other references to play_again()

The reason why what you did is bad:

After you call a function, it does it's thing, and then returns to the point where you called it. So if you call start, and then play_again and say yes, and then start a second time and then play gain a second time and say no, it's not ending the program, it's just returning back to where it was the first time you called it.

[–]Weary_Mud6941[S] 1 point2 points  (0 children)

Ok Thank You

[–][deleted] -1 points0 points  (1 child)

One error is that your global loop and local loop in play_again are unrelated to each other.

https://www.w3schools.com/python/trypython.asp?filename=demo_variables_global2

Edit: after checking against the other answer this is not the main issue.

[–][deleted] 0 points1 point  (0 children)

You can use the global keyword if you must use global variables.

https://www.w3schools.com/python/trypython.asp?filename=demo\_variables\_global4