【问题标题】:Codecademy Python 2 Rock, Paper, ScissorsCodecademy Python 2 石头、纸、剪刀
【发布时间】:2020-10-01 17:38:53
【问题描述】:

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

复习我的笔记并尝试理解/记住代码会变得很困难。

请解释一下如何处理这段代码。

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】:

一些注意事项 - elif user_choice == options[0] and computer_choice == options[1]: 应该是 elif user_choice == options[0] and computer_choice == options[2]: 您的 cmets 是正确的,但是您为 SCISSORS 索引了“1”而不是“2”。

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

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

decide_winner(user_choice, computer_choice)

【讨论】:

    【解决方案2】:
    1. Python 2 对变量等内容使用输入,对字符串等内容使用 raw_input。

    代替:

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

    使用

    user_choice = raw_input("Enter Rock, Paper, Scissors: ")
    
    1. 输入区分大小写 您的选择是
    options = ["ROCK", "PAPER", "SCISSORS"]
    

    但如果一个人输入“Rock”,则表明他们输了 而是改变

    user_choice = raw_input("Enter Rock, Paper, Scissors: ")
    

    到

    user_choice = raw_input("Enter Rock, Paper, Scissors: ").upper()
    

    【讨论】:

      【解决方案3】:

      从随机导入randint

      """这个程序将使用户和计算机开始一个石头剪刀布游戏"""

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

      message = {"tie": "打哈欠是平手!", "won": "是的,你赢了!", "lost": "哇,你输了!"}

      best_of_three = 3

      def decision_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: ").upper()
      
      computer_choice = options[randint(0, 2)]
      
      decide_winner(user_choice, computer_choice)
      

      play_RPS()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 2020-05-22
        • 1970-01-01
        相关资源
        最近更新 更多