Python3石头布剪刀环

2024-05-08 17:43:59 发布

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

当我运行程序时:

R,S or P?
Rock
you threw : R
your enemy threw : R
>>>

最后一行代码没有出现

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 ()

Tags: oryouyourifrandomcomputerprintgot
2条回答

用户选择是字符串,计算机选择是整数,所以比较总是失败的。你知道吗

更改自:

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!")

相关问题 更多 >

    热门问题