-2

The code works mostly well, however, after trying the play again command I made it exits the program and doesn't cycle as expected. The code causing issues is below.

play_again = 'y' or 'n'

draw_again = 'hit' or 'hold'

print("This is a simple game of blackjack. The main objective is to stay under or equal to 21 in value.")
print("Number cards are worth their number. Face cards are worth 11. Aces are worth either 1 or 11")
print("If you want to draw again type hit. To finish type hold.")


play_game = input("would you like to play? (y/n):")
if play_game == 'y':
    shuffleDeck()
    print ("your starting hand is")
    drawFaceUp()
    drawFaceUp()
    draw_again = input("hit, or hold (hit/hold):")

while draw_again == 'hit':
    print("your next card is")
    drawFaceUp()
    draw_again = input("hit, or hold (hit/hold):")

if draw_again == 'hold':
    score = input("Enter your score <score number>:")

if score <= '15':
    print("good job, but try and get a little closer to 21 next time")
    play_again = input("Play again? (y/n):")

    if play_again == 'y':
        newDeck()
        shuffleDeck()
        print ("your starting hand is")
        drawFaceUp()
        drawFaceUp()
        draw_again = input("hit, or hold (hit/hold):")

        while draw_again == 'hit':
            print("your next card is")
            drawFaceUp()
            draw_again = input("hit, or hold (hit/hold):")

        if draw_again == 'hold':
            score = input("Enter your score <score number>:")            

    elif play_again == 'n':
        print ("end of program")

elif score > '15' and score < '21':
    print("Nice. you should test you luck again.")
    play_again = input("Play again? (y/n):")

    if play_again == 'y':
        newDeck()
        shuffleDeck()
        print ("your starting hand is")
        drawFaceUp()
        drawFaceUp()
        draw_again = input("hit, or hold (hit/hold):")

        while draw_again == 'hit':
            print("your next card is")
            drawFaceUp()
            draw_again = input("hit, or hold (hit/hold):")

        if draw_again == 'hold':
            score = input("Enter your score <score number>:")            

    elif play_again == 'n':
        print("end of program")

elif score == '21':
    print("you got a perfect score. see if you can do it again.")
    play_again = input("Play again? (y/n):")

    if play_again == 'y':
        newDeck()
        shuffleDeck()
        print ("your starting hand is")
        drawFaceUp()
        drawFaceUp()
        draw_again = input("hit, or hold (hit/hold):")

        while draw_again == 'hit':
            print("your next card is")
            drawFaceUp()
            draw_again = input("hit, or hold (hit/hold):")

        if draw_again == 'hold':
            score = input("Enter your score <score number>:")        

    elif play_again == 'n':
        print("end of program")

elif play_game == 'n':
    print("end of program")

I expect the game to be able to endlessly cycle until told not to. the actual output causes the game to close after 2 rounds of playing.

  • 2
    `play_again = 'y' or 'n'` is the same as `play_again = 'y'`. – Barmar Apr 17 '19 at 17:17
  • 2
    Why do you assign the variables `play_again` and `draw_again` at the beginning of the script? You only use those variables for input from the user. – Barmar Apr 17 '19 at 17:18
  • If you want to be able to play the game multiple times, you should use a loop. – Barmar Apr 17 '19 at 17:20
  • 1
    Is this really the correct indentation of your coe? You go into the `while draw_again == 'hit':` loop even if the user answered `n` to "Would you like to play?" – Barmar Apr 17 '19 at 17:21
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. If your problem is only with looping the game, then the "insides" should not be in the posting. – Prune Apr 17 '19 at 17:44

1 Answers1

2

The way this code is structured, there isn't a main outer loop that keeps going until the user chooses to quit. (But you clearly didn't post the entire program, so maybe there is such a loop that you didn't show us?)

Instead, the user plays one game, sees a message based on their score, then can choose to play one more game, and that's all.

You might want to restructure the code so it all happens under one loop. This would also eliminate a lot of code repetition.

Perhaps something like this:

play_again = 'y'
while play_again == 'y':
    # play the game
    if score <= 15:
        print 'try harder'
    elif score <= 20:
        print 'not bad'
    elif score == 21:
        print 'great'
    play_again = input('Play again? ')
John Gordon
  • 29,573
  • 7
  • 33
  • 58