Python3石头剪纸循环

Python3石头剪纸循环,python,Python,当我运行程序时: 最后一行代码不显示 if user_choice == computer_choice : print ("tie!") 你知道我该怎么做吗 谢谢大家! import random def main(): user_choice = input("R,S or P?\n") user_choice = user_choice.lower() if user_choice == "rock" or user_choice

当我运行程序时:

最后一行代码不显示

if user_choice == computer_choice :
        print ("tie!")
你知道我该怎么做吗

谢谢大家!

import random


def main():

      user_choice = input("R,S or P?\n")
      user_choice = user_choice.lower()
      if user_choice == "rock" or user_choice == "r"  :
            print("you got R")
      elif user_choice == "p" or user_choice == "paper" :
            print("you got  P")
      elif user_choice == "s" or user_choice == "scissors" :
            print("you got S")
      else :
            print ("wrong answer!")

      computer_choice = random.randint(1,3)
      if computer_choice == 1 :
            print("your enemy chose  R")
      elif computer_choice == 2 :
            print("your enemy chose  P")
      else :
            print("your enemy chose S")


      if user_choice == computer_choice :
            print ("tie!")


main ()

用户选择是字符串,计算机选择是整数,因此比较总是失败。

更改为:

computer_choice = random.randint(1,3)
  if computer_choice == 1 :
        print("your enemy threw : R")
  elif computer_choice == 2 :
        print("your enemy threw : P")
  else :
        print("your enemy : S")

if user_choice == computer_choice :
        print ("It's a tie!")
致:


'r'
's'
'p'
这样的字符串怎么会等于像
1
2
3
这样的数字呢?
如果用户选择==“摇滚”或用户选择==“r”
如果用户选择[0]==“r”:
请不要破坏您自己的问题!请不要破坏你的帖子,为别人做更多的工作。通过在Stack Exchange(SE)网络上发布,您已根据授予SE分发该内容的不可撤销权利(即,无论您未来的选择如何)。根据SE政策,该帖子的非故意破坏版本是发布的版本。因此,任何故意破坏行为都将恢复原状。如果您想了解有关删除帖子的更多信息,请参阅:如果您继续这样破坏您的帖子,很可能会导致暂停。这是不够的,因为(正确的)
用户选择
。g
“rock”
@Mari当它询问
“R、S或P”\n“
”时,答案怎么可能是
“rock”
?@RickyKim:程序允许其他选择。查看代码:
如果user\u choice==“rock”或user\u choice==“r”:
@rickyim,你认为所有用户都很有纪律,程序员可能会依赖他们吗?@James是的,编辑的代码现在应该适用于所有这些奇怪的输入lol
computer_choice = random.randint(1,3)
  if computer_choice == 1 :
        print("your enemy threw : R")
  elif computer_choice == 2 :
        print("your enemy threw : P")
  else :
        print("your enemy : S")

if user_choice == computer_choice :
        print ("It's a tie!")
computer_choice = random.randint(1,3)
  if computer_choice == 1 :
        computer_choice = 'r'
        print("your enemy threw : R")
  elif computer_choice == 2 :
        computer_choice = 'p'
        print("your enemy threw : P")
  else :
        computer_choice = 's'
        print("your enemy : S")

if user_choice[0] == computer_choice :
        print ("It's a tie!")