Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2015
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    python rock paper scissors game help

    Whenever I select the 2nd main menu option to play against the computer and then select the 4th menu option in the weapons menu to return to the main menu I have an error when I try to quit. I have to enter the quit menu selection twice for it to work.

    here is the code and the .py file can be downloaded here teeest.py - Speedy Share - upload your files here :


    def main():
    import random
    pcWon = 0
    playerWon = 0
    playerTwo = 0
    mChoice = "0"
    end = "no"
    while end == "no":
    mChoice = menu(mChoice)
    if mChoice == "4":
    end = "yes"
    if mChoice == "1":
    rules()
    if mChoice == "2":
    cGame(pcWon, playerWon,mChoice)
    if mChoice == "3":
    twoGame(playerWon, playerTwo)









    def menu(mChoice):
    print("ROCK! PAPER! SCISSORS!")
    print("______________________________________________________________")
    print("Main Menu")
    print("\n1.\tSee the Rules")
    print("\n2.\tPlay against the computer")
    print("\n3.\tPlay a two player game")
    print("\n4.\tQuit")
    mChoice = input("What would you like to do? Please enter the number of the selection:\t")
    while (mChoice != "1" and mChoice != "2" and mChoice != "3" and mChoice != "4"):
    print("Please enter the number corrisponding with the menu option")
    mChoice = input("What would you like to do? Please enter the number of the selection:\t")

    return mChoice


    def rules():
    print("The Rules are as follows:\n_____________________________ \n")
    print("\nPaper Covers Rock\n")
    print("\nRock Smashes Scissors\n")
    print("\nScissors Cuts Paper\n\n")

    def cGame(pcWon, playerWon,mChoice):
    end = "no"
    while end == "no":
    import random
    print("\n___________________________________________________________________\nScore Board:\tComputer:\t",pcWon ,"\tPlayer 1:\t", playerWon,"\n___________________________________________________________________")

    print("Weapon Menu:\n\n1.Rock\n2.Paper\n3.Scissors\n4.Return to Main Menu")

    player_choice = input("Select your weapon by entering the number:\t")
    while (player_choice != "1" and player_choice != "2" and player_choice != "3" and player_choice != "4"):
    print("Please enter the number corrisponding with the menu option")
    player_choice = input("Select your weapon by entering the number:\t")

    comp_choice = random.randint(1,3)
    if comp_choice == 1:
    computer = "rock"
    elif comp_choice == 2:
    computer = "paper"
    elif comp_choice == 3:
    computer = "scissors"

    if player_choice == "1":
    player = "rock"
    elif player_choice == "2":
    player = "paper"
    elif player_choice == "3":
    player = "scissors"
    elif player_choice == "4":
    end = "yes"

    main()























    main()

  2. #2
    New Coder
    Join Date
    Nov 2016
    Location
    South Africa, Cape Town
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post
    This isn't your exact code but I created this based off of it and you can use it as a guide line:

    Code:
    import random
    
    global pcWon, playerWon, choice, keepPlaying
    
    pcWon = 0
    playerWon = 0
    choice = 0
    keepPlaying = True
    
    def rules():
        print "The Rules are as follows:\n_____________________________ \n"
        print "\nPaper Covers Rock\n"
        print "\nRock Smashes Scissors\n"
        print "\nScissors Cuts Paper\n\n"
    
    def cGame():
        global pcWon, playerWon
        
        while True:
                
            print "\n___________________________________________________________________\nScore Board:Computer:",pcWon ,"Player 1:", playerWon,"\n___________________________________________________________________"
            print "Weapon Menu:\n\n1.Rock\n2.Paper\n3.Scissors\n4.Return to Main Menu"
    
            print "\nPlease enter the number corrisponding with the menu option"
            
            player_choice = input("Select your weapon by entering the number:")
    
            if player_choice == 1:
                player = "rock"
            elif player_choice == 2:
                player = "paper"
            elif player_choice == 3:
                player = "scissors"
            elif player_choice == 4:
                return False
    
            comp_choice = random.randint(1,3)
            if comp_choice == 1:
                computer = "rock"
            elif comp_choice == 2:
                computer = "paper"
            elif comp_choice == 3:
                computer = "scissors"
    
            print "\nYou played " + player
            print "The computer played " + computer
    
            if player == "rock" and computer == "paper":
                pcWon += 1
            elif player == "paper" and computer == "rock":
                playerWon += 1
            elif player == "scissors" and computer == "paper":
                playerWon += 1
            elif player == "paper" and computer == "scissors":
                pcWon += 1
            elif player == "rock" and computer == "scissors":
                playerWon += 1
            elif player == "scissors" and computer == "rock":
                pcWon += 1
            elif player == "scissors" and computer == "scissors":
                print "Tie"
            elif player == "rock" and computer == "rock":
                print "Tie"
            elif player == "paper" and computer == "paper":
                print "Tie"
    
    def menu():
        global keepPlaying
        
        while keepPlaying:
    
            print "ROCK! PAPER! SCISSORS!"
            print "______________________________________________________________"
            print "Main Menu"
            print "\n 1.See the Rules"
            print "\n 2.Play against the computer"
            print "\n 3.Quit\n"
            
            choice = input("What would you like to do? Please enter the number of the selection:")
            
            if choice == 1:
                rules()
            elif choice == 2:
                cGame()
            elif choice == 3:
                keepPlaying = False
    
    menu()
    The reason you had to type exit multiple time was because of nested while loops and incorrect placement of user input. What your code was doing is it was break out of the inner most while loop and then running the outer most, and because the outer most while loop contained an input() statement it caused you to have to repeat the exit. A good future note is to always use globals as apposed to having parameters parsed, in this case that is. That way the globals are declared upon program start and can be can retain their updated values throughout the program continuation without risk of being reset unless explicit done. Also always declare your functions prior to reference, even from within another function, globals come in handy here as well because you won't receive any unresolved reference errors and they will not be modified because said function will not be run until the very last function, in this the case the menu, is run.

  3. #3
    New Coder
    Join Date
    Nov 2016
    Location
    South Africa, Cape Town
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post
    Also note that this code can be further reduced by using smarter functions to check values but this will do what you wanted.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •