while within while

Shawn Minisall trekker182 at comcast.net
Sat Oct 27 15:11:37 EDT 2007


I've been having some problems with using a while statement for one menu 
within another while statement for the main menu, first time I've done 
it.  It's with choice number two from the menu.  When I run the program, 
I get a UnboundLocalError: local variable 'ai' referenced before 
assignment.  I initialize ai as "", but then it just skips to the you 
choose scissors I choose and nothing shows up.  As soon as I take away 
the while again[0] == "y": statement, the program is fine again which 
leads me to think I'm just placing it in the wrong place.  I just want 
to ask the user if they would like to play again and loop them back to 
the weapons menu if they choose yes.  If they choose no,  loop them back 
to the main menu.  I've placed the question again=raw_input("Would you 
like to play again? ") at the end the tasks for menu choice two.  Ignore 
three....one step at a time. ;)

thx

import random


def main():

    #define and initialize variables
    #choice as int
    choice = 0
    #weapon choice as int
    weaponchoice = 0
    #number of wins
    win = 0
    #number of loses
    lose = 0
    #number of ties
    tie = 0
    #number of rounds
    rounds = 0
    #play again loop
    again = "no"

 

    #intro
    print "READY TO PLAY ROCK, PAPER, SCISSORS???"

    print
   
   
    #Menu loop
    while choice != 4:
        #display menu
        print "Please choose from the following menu: "
        print "1. See the rules"
        print "2. Play against the computer"
        print "3. Play a two player game"
        print "4. Exit"
       
        #prompt user for their menu choice
        choice = input("Please enter your choice here: ")
        print
           
       

        #if statements to determine which choice
        if choice == 1:
            print
            print "The rules of the game are as follows: "
            print
            print "Rock Covers Rock"
            print
            print "Rock Smashes Scissors"
            print
            print "Scissors Cuts Paper"
            print
            print
       
       
        elif choice == 2:
            while again[0] == "y":
                #display menu
                print "Please choose a weapon from the following menu: "
                print "1. Rock"
                print "2. Paper"
                print "3. Scissors"

                while weaponchoice != 1 and weaponchoice != 2 and 
weaponchoice != 3:
                    weaponchoice = input("Please choose a weapon: ")
                    if weaponchoice != 1 and weaponchoice != 2 and 
weaponchoice != 3:
                        print
                        print "Error. Please enter a number from 1-3."

                decision = (1, 2, 3)
                ai = str((random.choice(decision)))

                if ai == "1":
                    ai = "rock"
                if ai == "2":
                    ai = "paper"
                if ai == "3":
                    ai = "scissors"

            if weaponchoice == 1:
                weaponchoice = "rock"

            elif weaponchoice == 2:
                weaponchoice = "paper"

            else:
                weaponchoice = "scissors"

            print "====================="
            print "you choose " + weaponchoice
            print
            print "I choose " + ai
            print

            if weaponchoice == "rock" and ai == "scissors":
                win += 1
                print "You WIN by SMASHING those SCISSORS!"

            elif weaponchoice == "paper" and ai == "rock":
                win += 1
                print "You WIN by COVERING that ROCK!"

            elif weaponchoice == "scissors" and ai == "paper":
                win += 1
                print "You WIN by CUTTING that PAPER!"

            elif weaponchoice == ai:
                tie += 1
                print "YOU TIE!"

            else:
                lose += 1
                print "YOU LOSE!"

            print "\nRounds Won: ", + win
            print "\nRounds Lost: ", + lose
            print "\nRounds Tied: ", + tie
            print
            print

            again=raw_input("Would you like to play again? ")


        elif choice == 3:
            print "test"  
      
        elif choice == 4:
            print "Have a great day!"
            print
            print "Thanks for playing!"

        else:
            #invalid
            print "Invalid selection.  Please enter a number from 1 - 4."
            print





More information about the Python-list mailing list