我的Python中的If语句无法正常工作

时间:2019-07-09 19:44:19

标签: python if-statement

我是python的新手,我正在编写一个程序来播放Rock,Paper和Scissor。当我开始执行if else语句以比较用户和计算机的输出时,我的程序完全跳过了if-else语句。不知道这是怎么回事。

user = input("Please enter Rock, Paper, Scissor: ")
while user not in ("Rock", "Paper", "Scissor"):
      print("Try again")
      user = input("Please enter Rock, Paper, Scissor: ")


if user == "Rock":
   print("Your Choice is Rock")
elif user == "Scissor":
   print("Your Choice is Scissor")
elif user == "Paper":
   print("Your Choice is Paper")


import random
AI=["Rock", "Paper", "Scissor"]
b=random.randint(0,2)
print("The computer choice is " + AI[b])

if user == b:
   print("Tie")
elif user == "Rock":
   if b == "Scissor":
      print("Rock Beats Scissor")

它遍历了所有代码,期望最后一个if-else语句。在计算机选择了要使用的程序后,它才完成程序。

1 个答案:

答案 0 :(得分:1)

您的问题在最后一个if中,您正在将字符串与整数user == b进行比较,请记住,用户选择是字符串,但是b是0到2之间的随机数

if user == AI[b]:  # you need to compare the value with the string selection of the "AI"
   print("Tie")
elif user == "Rock":
   if AI[b] == "Scissor":
      print("Rock Beats Scissor")
else:
   # I don't remember what are the rules to win or lose LOL, but I guess there are more
   # maybe you need more conditions
   print("other choice...")