我刚刚开始编码,每天都像婴儿一样成长
很难,但是请查看我的笔记并尝试理解/记住代码。
请为我解释该代码的处理方法。
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()
[一些注意事项-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)
Python 2将输入用于变量之类,并将raw_input用于字符串之类。