Codecademy Python 2 Rock,Paper,Scissors

问题描述 投票:-1回答:1

我刚刚开始编码,每天都像婴儿一样成长

很难,但是请查看我的笔记并尝试理解/记住代码。

请为我解释该代码的处理方法。

from random import randint
"""This program will enable the user and computer to start a rock paper scissors game"""

options = ["ROCK", "PAPER", "SCISSORS"]

message = {"tie": "Yawn it's a tie!", "won": "Yay you won!", "lost": "Aww you lost!"}


def decide_winner(user_choice, computer_choice):
    print("You chose %s") % (user_choice)

    print("PC chose %s") % (computer_choice)

    if user_choice == computer_choice:
        print(message["tie"])  # tie

    # user - paper , pc = rock
    elif user_choice == options[1] and computer_choice == options[0]:
        print(message["won"])

    # user - scissor , pc = paper
    elif user_choice == options[2] and computer_choice == options[1]:
        print(message["won"])

    # user - rock , pc - scissors
    elif user_choice == options[0] and computer_choice == options[1]:
        print(message["won"])

    else:
        print("YOU LOSE!")


def play_RPS():
    user_choice = input("Enter Rock, Paper, Scissors: ")

    computer_choice = options[randint(0,2)]
    decide_winner(user_choice, computer_choice)

play_RPS()
python string list int
1个回答
0
投票

[一些注意事项-elif user_choice == options[0] and computer_choice == options[1]:应该为elif user_choice == options[0] and computer_choice == options[2]:您的评论是正确的,但对于SCISSORS,您将索引编为'1'而不是'2'。

Python区分大小写,因此'ROCK'将不等于'Rock'。您可能需要添加行

[user_choice = user_choice.upper()在函数def play_RPS():中传递给]之前>

decide_winner(user_choice, computer_choice)


0
投票

Python 2将输入用于变量之类,并将raw_input用于字符串之类。

© www.soinside.com 2019 - 2024. All rights reserved.