岩石剪刀,跳过结果

Avi*_*ian 3 python random

import random
for i in range(3):
user = str(input("Please enter your choice: "))
if (random.randrange(3)) == 0 :
    print("Computer chooses Rock")
    if user == "scissors" :
        print("computer wins")
    elif user == "paper" :
        print("player wins")
    else :
        print("tie")
elif (random.randrange(3)) == 1 :
    print("Computer chooses Paper")
    if user == "rock" :
        print("computer wins")
    elif user == "scissors" :
        print("player wins")
    else :
        print("tie")
elif (random.randrange(3)) == 2 :
    print("Computer chooses Scissors")
    if user == "paper" :
        print("computer wins")
    elif user == "rock" :
        print("player wins")
    else :
        print("tie")
Run Code Online (Sandbox Code Playgroud)

这里的格式有点奇怪(之前没有使用过这个网站).我不知道原因,但我不知道为什么这段代码有时会跳过结果.如果有人能提供帮助,那就太好了.

这是运行几次时产生的结果

enter your choice: scissors
Computer chooses Rock
computer wins
enter your choice: scissors
Computer chooses Scissors
tie
enter your choice: scissors
Computer chooses Rock
computer wins
================================ RESTART ================================ 
Please enter your choice: scissors
Please enter your choice: rock
Computer chooses Rock
tie
Please enter your choice: rock
Computer chooses Rock
tie
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它会跳过结果.似乎是随机发生的

And*_*ner 5

你不应该使用random.randrange(3)三次.这可能会给你以下数字:1,2,然后是0.所以然后执行的代码将是:

if (1 == 0):
   ...
elif (2 == 1):
   ...
elif (0 == 2):
   ...
Run Code Online (Sandbox Code Playgroud)

并且不会执行if语句的任何条件块.

而是做这样的事情:

computerChoice = random.randrange(3)
...
if computerCoice == 0:
  ...
elif computerChoice == 1:
  ...
elif computerChoice == 2:
  ...
else
  raise Exception("something is definitively wrong here")
Run Code Online (Sandbox Code Playgroud)