簡體   English   中英

Python 3石頭剪刀布循環

[英]Python 3 rock Paper Scissors loops

當我運行程序時:

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

用戶選擇是字符串,計算機選擇是整數,因此比較總是失敗。

從:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM