2014-11-22 59 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! 
>>>