def冒号语法错误

2024-04-23 20:59:48 发布

您现在位置:Python中文网/ 问答频道 /正文

运行此代码时,原始def函数中的冒号出现语法错误。我真的不知道为什么。在

#Rock Paper Scissors
import random;
def rps:
    user = input("Enter your choice (rock/paper/scissors): ");
    user = user.lower();
    while (user != "rock" and user != "paper" and user != "scissors"):
        print(user);
        user = input("That choice is not valid. Enter your choice (rock/paper/scissors): ");
        user = user.lower();

    computerInt = random.randint(0,2);
    if (computerInt == 0):
        computer = "rock";
    elif (computerInt == 1):
        computer = "paper";
    elif (computerInt == 2):
        computer = "scissors";
    else:
        computer = "Error";

    if (player == computer):
        print("Draw!");
    elif (player == "rock"):
        if (computer == "paper"):
            print("Computer wins!");
        else:
            print("You win!");
        elif (player == "paper"):
            if (computer == "rock"):
                print("You win!");
            else:
                print("Computer wins!")
            elif (player == "scissors"):
                if (computer == "rock"):
                    print("Computer wins!");
                else:
                    print("You win!");

    print("Your choice: " + player + "\nComputer choice: " + computer + "\nThank you for playing!");
    playAgain = input("Would you like to play again? (y/n)");
    playAgain = playAgain.lower()
    if (playAgain == "y"):
        rps()
    else:
        print("Thank you for playing.")
        input("Press any key to exit")

Tags: inputiflowerelsecomputerpaperplayerprint