I'm looking for some major help.
I'm a decently new coder, taking a Python class independent study.
I was writing a program for a two-player 21 game. (Like blackjack only I start with only one card... for some reason.)
Only problem is, while the code doesn't seem to contain any errors, it loops on two lines of print in one of my while loops, infinitely...
(Now to hope posting code doesn't do anything stupid...)
import random
playAgain = 'yes'
print "This is a 2-player 21 game... Hope you enjoy!"
while playAgain == 'yes' or playAgain == 'y':
# The loop starts by resetting all variables used to the default they need to work with the beginning of the game.
turn1 = True
turn2 = True
stand1 = False
stand2 = False
busted1 = False
busted2 = False
game = True
gameEnd1 = False
gameEnd2 = False
player1 = raw_input("Player 1, enter your name: ")
player2 = raw_input("Player 2, enter your name: ")
numberStored1 = random.randint(1, 11)
numberStored2 = random.randint(1, 11)
print player1 + ", your first card gives you a hand value of " + str(numberStored1) + "."
print player2 + ", your first card gives you a hand value of " + str(numberStored2) + "."
while game: # This says when the game ends, so that it may display endgame information.
while turn1 == True:
turnAction = raw_input(str(player1) + ", do you wish to hit or stand? ")
if turnAction == "hit":
numberStored1 = numberStored1 + random.randint(1, 11)
turn1 = False
elif turnAction == "stand":
numberStored1 = numberStored1
turn1 = False
stand1 = True
else:
print "Incorrect input. Please enter 'hit' or 'stand' to continue."
while turn2 == True:
turnAction = raw_input(str(player2) + ", do you wish to hit or stand? ")
if turnAction == "hit":
numberStored2 = numberStored2 + random.randint(1, 11)
turn2 = False
elif turnAction == "stand":
numberStored2 = numberStored2
turn2 = False
stand2 = True
else:
print "Incorrect input. Please enter 'hit' or 'stand' to continue."
# It seems to enjoy looping the next two lines infinitely. This is my main issue right now.
print player1 + ", you currently have a hand value of " + str(numberStored1) + "."
print player2 + ", you currently have a hand value of " + str(numberStored2) + "."
# All the remaining code lines within the 'game' loop
# should account for all possible outcomes at the end of a turn.
# I double checked this part, but if you see somewhere that causes the print loop above,
# Please point it out, thank you.
if busted1 == False and busted2 == False:
if numberStored1 > 21 and numberstored2 <= 21:
print str(player1) + " went bust! "
busted1 = True
if stand2 == False:
print str(player2) + " may still take turns."
turn2 = True
elif numberStored2 > 21 and numberstored1 <= 21:
print str(player2) + " went bust! "
busted2 = True
if stand1 == False:
print str(player1) + " may still take turns."
turn1 = True
elif numberStored1 > 21 and numberStored2 > 21:
print "Both players went bust. Oh well!"
busted1 = True
busted2 = True
else:
if stand1 == True and stand2 == False:
print str(player2) + " still has a decision."
turn2 = True
elif stand2 == True and stand1 == False:
print str(player1) + " still has a decision."
turn1 = True
elif stand1 == True and stand2 == True:
pass
elif busted1 == True:
if numberStored2 > 21:
print "Oh, " + str(player2) + " went bust as well! Players tie in bust."
elif stand2 == False:
print str(player2) + " still has a decision, then!"
turn2 = True
elif busted2 == True:
if numberStored1 > 21:
print "Oh, " + str(player1) + " went bust as well! Players tie in bust."
elif stand1 == False:
print str(player1) + " still has a decision, then!"
turn1 = True
if busted1 == True or stand1 == True:
gameEnd1 = True
if busted2 == True or stand2 == True:
gameEnd2 = True
if gameEnd1 == True and gameEnd2 == True:
game = False
# Here there be endgame info, yarr.
# ... and possibly a coder with a lack of sleep and pirates on the brain.
print "The game is over!"
if busted1 == True:
print str(player1) + " went bust with a score of " + str(numberStored1) + "."
else:
print str(player1) + " had a final score of " + str(numberStored1) + "."
if busted2 == True:
print str(player2) + " went bust with a score of " + str(numberStored2) + "."
else:
print str(player2) + " had a final score of " + str(numberStored2) + "."
if busted1 == False and busted2 == False:
if numberStored1 > numberStored2:
print str(player1) + " is the winner!"
elif numberStored2 > numberStored1:
print str(player2) + " is the winner!"
elif busted1 == True:
print str(player2) + " is the winner!"
elif busted2 == True:
print str(player1) + " is the winner!"
print "Good game, " + str(player1) + " and " + str(player2) + "!"
playAgain = raw_input("Would you like to play again? (yes/y or no/n) ")
# Ah, my favorite. Almost my little signature.
# Add it to the end of all of my programs in some way or form.
# Saw the idea used once in a tutorial, but the problem
# was it never explained the .lowercase thing for input, so I just made it into this.
# ... not that this is helping my loop any.
I hope and pray that somehow you can help me with this. Thanks a million to anyone able to see what I did wrong.
~ Pixel