2014-11-22 21 views
3
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") 

这里的格式化有点奇怪(之前没有使用过此网站)。我不知道原因,但我不知道为什么这段代码有时会跳过结果。如果任何人都能帮上忙,那会很棒。岩石剪刀,跳过结果

这是当它运行几次

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 

我不明白为什么它会跳过结果生产什么。似乎发生随机

+1

计算机是一种欺骗行为...只允许一个随机选择,在你进入'if'链之前。顺便说一句,有更简单的方法来确定一方面的结果。 – gboffi 2014-11-22 17:20:54

回答

5

你不应该使用random.randrange(3)三次。这可以例如为您提供以下数字:1,2,然后0。因此,然后执行的代码将是:

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

,没有的if语句将被执行的条件块的。

而是做这样的事情:

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

你如何建议我修复它?将随机分配给一个变量? – Avian 2014-11-22 17:18:43

+0

非常感谢,对不起,我是一个杯子。 – Avian 2014-11-22 17:19:45

2

你已经使用random.randrange(3)多次,每次有它是一个不同数量的可能性。所以我建议你值分配给一个变量,然后在您的if报表中使用:

x = random.randrange(3) 
1

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

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 
0

你的错误是在这里:

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

在if语句random.randrange(3)会产生一个随机数,如果不匹配,它会去ELIF语句和随机数.randrange(3)将会生成另一个随机数,它可能与以前生成的随机数不一样。由于这个原因,有些时候你的代码会跳过一个,两个,三个或者没有任何一个声明。

如果你想你的代码是好的,你应该分配的随机数为整数,然后使用你的答案是整数。

应该

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

2

我看到你已经接受了安德烈的出色答卷,这需要你清楚地了解你的错误。正如我评论您的问与答说,有奖励简单的方法一只手,这是我采取的

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] 

这是一个例子会话:

>>> hand() 
1=scissors, 2=paper, 3=rock: 3 
User played rock, computer played scissors. 
User wins! 
>>>