Python 石头剪刀,跳过一个结果

Python 石头剪刀,跳过一个结果,python,random,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"

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

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

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

我不明白为什么它忽略了一个结果。似乎是随机发生的

您不应该使用
random.randrange(3)
三次。例如,这可能会给您以下数字:1、2和0。因此,随后执行的代码将是:

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
if语句的任何条件块都不会执行

相反,你可以这样做:

if (1 == 0):
   ...
elif (2 == 1):
   ...
elif (0 == 2):
   ...

您多次使用了
random.randrange(3)
,每次都可能是不同的数字。。因此,我建议您将该值赋给一个变量,然后在
if
语句中使用该值:

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

是的,这是随机发生的。:)

你的错误在这里:

if(random.randrange(3))==0:
elif(random.randrange(3))==1:
elif(random.randrange(3))==2:

在if语句random.randrange(3)中,如果它不匹配,将生成一个随机数,它将转到elif语句并random.randrange(3)将生成另一个随机数,它可能与以前生成的随机数不同。由于这个原因,您的代码有时会跳过一条、两条、三条或一条都不跳过

如果你希望你的代码是好的,你应该把随机数分配给一个整数,然后在你的答案中使用这个整数

应该是

ran=random.randrange(3)
如果ran==0:
elif ran==2:

elif ran==3:

我看你已经接受了Andre的优秀答案,这让你清楚地理解了自己的错误。正如我评论你的Q说有更简单的方法来奖励一只手,这是我的看法

if (random.randrange(3)) == 0 :
  # computer "chooses" a random number
elif (random.randrange(3)) == 1 :
  # now the computer's choice is a new choice from 0..2
elif (random.randrange(3)) == 2 :
  # and now it's different again
但我不确定它是否更简单。。。当然更短,但更简单

你可以把它做得更短

import random

c = random.randrange(3)
u = int(input('1=scissors, 2=paper, 3=rock: '))-1

if u==c:
    print '... tie ...'
elif (u==0 and c==1) or (u==1 and c==2) or (u==2 and c==0):
    print 'User wins!'
else:
    print 'Computer wins... Booh!'
这是一个示例会话:

import random

def hand():
    c = random.randrange(3)
    u = int(input('1=scissors, 2=paper, 3=rock: '))-1
    print "User played",['scissors,', 'paper,', 'rock,'][u],
    print "computer played",['scissors.', 'paper.', 'rock.'][c]
    print ['Tie.', 'User wins!', 'Computer wins...'][(c-u)%3]

你建议我怎么修?随机分配一个变量?非常感谢,对不起,我是个傻瓜。计算机在作弊。。。在进入
if
s链之前,只允许对其进行一个随机选择。顺便说一句,有更简单的方法来确定一只手的结果。
import random

def hand():
    c = random.randrange(3)
    u = int(input('1=scissors, 2=paper, 3=rock: '))-1
    print "User played",['scissors,', 'paper,', 'rock,'][u],
    print "computer played",['scissors.', 'paper.', 'rock.'][c]
    print ['Tie.', 'User wins!', 'Computer wins...'][(c-u)%3]
>>> hand()
1=scissors, 2=paper, 3=rock: 3
User played rock, computer played scissors.
User wins!
>>>