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

Wow thats a mess of code, try sorting some stuff into functions to make a little more clear first off, I can take a look at it later. Also I'd move the portion where you select your player name out before the loop so you don't have to set it again after every loop.

Member Avatar for masterofpuppets

i think you problem is here:

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

first you have an error in line 61 numberstored1 should be numberStored1 :) and here:

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

i think you need to add another check whether stand1 == False and stand2 == False because if both players hit there is no check for that( or i didn't see it :) ) add that check and it should stop the infinite loop hopefully, i'll try to check the other part of the code when i get home :) hope that helps for now

Member Avatar for masterofpuppets

i agree with ov3rcl0ck, try to use a function for each player, it'll be a lot easier to write and to read as well.

Wow thats a mess of code, try sorting some stuff into functions to make a little more clear first off, I can take a look at it later. Also I'd move the portion where you select your player name out before the loop so you don't have to set it again after every loop.

i agree with ov3rcl0ck, try to use a function for each player, it'll be a lot easier to write and to read as well.

Ah, yes...
Now comes the big learning step.
I know how to define functions, but... could you possibly give me a hint as to how I could make this into separate functions for each player?
I mean... this is technically based off a one player simulation, not really a game, as it automatically does everything for you...
... YAY learning time. >.<; *starts to figure this out while constantly checking back for responses, wow this is going to be confusing*

EDIT: Or... Wow I feel stupid. Puppets, the lack of double hit code was exactly what I was missing. It runs now! YAY!!! Now... I still think I can -improve- my code... So the question above is still valid. But at least it works.

Member Avatar for masterofpuppets

ok, right...here's what I came up with last night. It's not 100% complete but can give you some idea on what could be improved...:)

from random import randint

#Gets and returns the names of the players
def getNames():
    print "*********************************************"
    player1 = raw_input( "Player 1, enter your name: " )
    player2 = raw_input( "Player 2, enter your name: " )
    print
    return player1, player2

#Picks a random card number and adds it to the current hand
def hit():
    return randint( 1, 11 )

#Checks if a player goes 'bust'
def checkBust( player1Nums, player2Nums ):
    if player1Nums > 21 and player2Nums <= 21:
        return 1
    elif player2Nums > 21 and player1Nums <= 21:
        return 2
    elif player1Nums > 21 and player2Nums > 21:
        return "both"
    else:
        return "none"

#Available options for each player
def takeTurn( player ):
    turnAction = raw_input( player + " do you wish to 'hit' or 'stand'? >> " )
    while turnAction not in [ "hit", "stand" ]:
        print "Wrong input"
        turnAction = raw_input( player + " do you wish to 'hit' or 'stand'? >> " )
    return turnAction

def printStatus( pl, value ):
    print  pl + ", you currently have a hand value of " + str( value ) + "."

#Restart or quit
def endGame():
    new_game = raw_input( "New game? ( y/n ) >> " )
    print
    if new_game == "y":
        a, b = getNames()     
        startGame( a, b )

#Deal with the situation where one player is bust and the other is stand
def checkBustAndStand( p1Bust, p1Stand, p2Bust, p2Stand, p1, p2, res1, res2 ):
    p1Bust, p2Stand, p1Stand, p2Bust
    if p1Bust and p2Stand:
        return p2 + " wins the game!"
    elif p1Stand and p2Bust:
        return p1 + " wins the game!"
    elif p1Stand and p2Stand:
        if res1 > res2:
            return p1 + " wins the game!"
        elif res1 < res2:
            return p2 + " wins the game!"
        else:
            return "Game ends in tie!"
    return False

#Main game function
def startGame( p1, p2 ):
    numberStored1 = randint( 1, 11 )
    numberStored2 = randint( 1, 11 )
    game, turn1, turn2, bust1, bust2, stand1, stand2 = True, True, True, False, False, False, False

    print p1, "your first card gives you a hand value of", numberStored1
    print p2, "your first card gives you a hand value of", numberStored2

    while game:
        if type( checkBustAndStand( bust1, stand1, bust2, stand2, p1, p2, numberStored1, numberStored2 ) ) == type( "s" ):
            print checkBustAndStand( bust1, stand1, bust2, stand2, p1, p2, numberStored1, numberStored2 )
            break
        if turn1 and not stand1:
            turn1 = False
            action1 = takeTurn( p1 )
            if action1 == "hit":
                numberStored1 += hit()
            else:
                stand1 = True

        if turn2 and not stand2:
            turn2 = False
            action2 = takeTurn( p2 )
            if action2 == "hit":
                numberStored2 += hit()
            else:
                stand2 = True
        print
        printStatus( p1, numberStored1 )
        printStatus( p2, numberStored2 )

        busted = checkBust( numberStored1, numberStored2 )
        if busted == "none":
            turn1 = True; turn2 = True
        elif busted == "both":
            print "Players tie in bust."
            game = False
        elif busted == 1:
            turn2 = True
            if not bust1:
                print p1, "went bust!"
            else:
                game = False
            bust1 = True
        elif busted == 2:
            turn1 = True
            if not bust2:
                print p2, "went bust!"
            else:
                game = False
            bust2 = True
    endGame()
    
a, b = getNames()     
startGame( a, b )

it's not the best algorithm for the game, but i think it works ( tested it ) but if you find a bug post a message. :) hope this helps you!

commented: Wow... thanks, man. This is like... -the- best thing you could do for me. Now that I know a little more about defining functions for uses, and of course getting to learn new functions (type() is going to be fun to find out about)? More learning! +0

ok, right...here's what I came up with last night. It's not 100% complete but can give you some idea on what could be improved...:)

*snip for space*

it's not the best algorithm for the game, but i think it works ( tested it ) but if you find a bug post a message. :) hope this helps you!

I like this, thank you! I'm going to add some stuff, change some stuff, but all changes are almost 100% print, nothing function-wise.
A main interest I see with this code is the use of the type() function... one I've never seen before. I'll have to look this up, and if it does anything I do manually a lot, or something I've been trying to do for a long time, I can use it. Thanks!

... and may I say I freaking love this website? And python, too. It's so easy to learn! It kinda makes you -want- to learn programming.

...
>.>
<.<

import present
for users in userlist:
    while users == awesome:
        user = users + present

... yep, python is really easy. ^.^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.