Python中的岩纸剪刀游戏

时间:2017-04-30 19:33:27

标签: python-3.x

我目前正在为我的编程课程介绍完成一个项目,并且我遇到了这个菜单驱动游戏的问题。它给了我一个无效的语法错误。我已经包含了代码,非常感谢任何帮助!非常感谢。 道格

import random
p1 = 0
p2 = 0
cpu = 0
tie = 0
def main():
    menuSelect = ""
    print("Welcome to Rock, Paper, Scissors!\n")

# main menu
    print("Main Menu")
    print("1. See the rules")
    print("2. Play against the computer")
    print("3. Play a two player game")
    print("4. Quit\n")

    menuSelect = int(input("Please make choice 1-4:\t"))

    while menuSelect < 1 or menuSelect > 4:
        print("The selection provided is invalid.\n")
        menuSelect = int(input("Please select one of the four options:\t"))

    if menuSelect == 1:
        rules()
    elif menuSelect == 2:
        onePlayer()
    elif menuSelect == 3:
        twoPlayer()
    elif menuSelect == 4:
        endGame()

# display the rules to the user
def rules():

    print("\n\nRules")
    print("________________________")
    print("Paper Covers Rock")
    print("Rock Smashes Scissors")
    print("Scissors Cut Paper\n")
    main()


# one player mode
def onePlayer():
    p1 = 0
    p2 = 0
    cpu = 0
    tie = 0
    again = ""
    player = False

    print("\nPlayer VS Computer")

    while player == False:
        print("Select a Weapon!")
        print("1. Rock")
        print("2. Paper")
        print("3. Scissors")
        print("4. Return to Main Menu")
        player = int(input("\nWhat is your weapon of choice(1-4):\t"))

        #computer = WEAPON[randint(0,2)]
        #temporary
        computer = random.randint(1,3)

        if player == computer:
            print("It's a tie!\n")
            tie = tie + 1
        elif player == 1:
            if computer == 2:
                print("Paper covers rock! You lose!\n")
                cpu = cpu + 1
            else:
                print("Rock smashes scissors. You win!\n")
                p1 = p1 + 1
        elif player == 2:
            if computer == 3:
                print("Scissors cut paper! You lose!\n")
                cpu = cpu + 1
            else:
                print("Paper covers rock. You win!\n")
                p1 = p1 + 1
        elif player == 3:
            if computer == 1:
                print("Rock smashes scissors! You lose!\n")
                cpu = cpu + 1
            else:
                print("Scissors cut paper. You win!\n")
                p1 = p1 + 1
        else:
            print("invalid input")

        print("Scores for this session:\n")
        print("Ties:\t", tie)
        print("Computer:\t", cpu)
        print("Player 1:\t", p1)

        again = input("Would you like to play again? Yes or No\n")
        again = again.lower()

        if again=="yes" or again=="y":
            player = False
        else:
            player = True
            main()


def twoPlayer():
    p12 = 0
    p2 = 0
    tie2 = 0
    again = ""
    player1 = False
    player2 = 0

    print("\nPlayer VS Computer")

    while player1 == False:
        print("Select a Weapon!")
        print("1. Rock")
        print("2. Paper")
        print("3. Scissors")
        print("4. Return to Main Menu")
        player1 = int(input("\nPlayer 1 what is your weapon of choice(1-4):\t"))
        player2 = int(input("\n\nPlayer 2 what is your weapon of choice(1-4):\t")

        if player1 == player2:
            print("It's a tie!\n")
            tie2 = tie2 + 1
        elif player1 == 1:
            if player2 == 2:
                print("Paper covers rock! You lose!\n")
                p2 = p2 + 1
            else:
                print("Rock smashes scissors. You win!\n")
                p12 = p12 + 1
        elif player1 == 2:
            if player2 == 3:
                print("Scissors cut paper! You lose!\n")
                p2 = p2 + 1
            else:
                print("Paper covers rock. You win!\n")
                p12 = p12 + 1
        elif player1 == 3:
            if player2 == 1:
                print("Rock smashes scissors! You lose!\n")
                p2 = p2 + 1
            else:
                print("Scissors cut paper. You win!\n")
                p12 = p12 + 1
        else:
            print("invalid input")

        print("Scores for this session:\n")
        print("Ties:\t", tie2)
        print("Player 1:\t", p12)
        print("Player 2:\t", p2)

        again = input("Would you like to play again? Yes or No\n")
        again = again.lower()

        if again=="yes" or again=="y":
            player1 = False
        else:
            player1 = True
            main()


main()

1 个答案:

答案 0 :(得分:0)

在代码的这一部分,在你的&#34;结束时,播放器1 ==假:&#34;循环:

player2 = int(输入(&#34; \ n \ nPlayer 2你选择的武器是什么(1-4):\ t&#34;)

只需插入另一个括号即可运行