2016-02-28 118 views
1

我想要while循環來改變每次迭代的數字(count和random int),但是當我運行程序時,循環只會繼續計數上的相同數字,隨機INT:在while循環中計數/隨機int

# if 4 sides 
die1 = random.randint(1,4) 
die2 = random.randint(1,4) 
count = 1 

while sides == 4 and die1 != die2: 
    print (count, ". die number 1 is", die1, "and die number 2 is", die2,".") 
    count == count + 1 

print ("You got snake eyes! Finally! On try number", count,".") 
+2

蛇眼睛在骰子游戲通常是指那些對的,不只是任何一對。 –

+0

爲了擺脫輸出中的無用空白,考慮'print(「{}。模子1是{},模子2是{}。」。format(count,die1,die2))''。 –

回答

2

您還需要做while循環內的random電話,否則他們將不會改變。另一件事是,你比較==,而不是設置=計數器:

import random 

sides = 4 
count = 1 
die1 = random.randint(1,4) 
die2 = random.randint(1,4) 

while sides == 4 and die1 != die2: 
    print (count, ". die number 1 is", die1, "and die number 2 is", die2,".") 
    count += 1 
    die1 = random.randint(1,4) 
    die2 = random.randint(1,4) 

print ("You got snake eyes! Finally! On try number", count,".") 

試圖測試運行給我:

1 . die number 1 is 4 and die number 2 is 3 . 
2 . die number 1 is 2 and die number 2 is 1 . 
3 . die number 1 is 1 and die number 2 is 2 . 
4 . die number 1 is 3 and die number 2 is 4 . 
5 . die number 1 is 1 and die number 2 is 4 . 
You got snake eyes! Finally! On try number 6 . 

一種選擇,這幾乎是相同的,但在使用break而不是條件while循環將是:

import random 

sides = 4 
count = 1 

def tossdie(): 
    """Function to create a random integer for a die""" 
    return random.randint(1,4) 

while True: 
    die1 = tossdie() 
    die2 = tossdie() 
    print (count, ". die number 1 is", die1, "and die number 2 is", die2,".") 
    if die1 == die2: 
     break 
    count += 1 

print ("You got snake eyes! Finally! On try number", count,".") 
4

每次電話random.randint(1,4),要創建一個隨機數的時間。無論何時打印,它都不會奇蹟般地變爲新的隨機數。

在您的while循環內生成新的隨機數字random.randint(1, 4)

第二個問題是count == count + 1檢查是否相等(並在您的情況下返回False)。要執行分配,請使用賦值運算符=count += 1count加1。

如果你想要一臺發電機是無休止地吐出隨機數,寫一個:

>>> import random 
>>> def rng(i, j): 
...  while True: 
...   yield random.randint(i, j) 
... 
>>> random_gen = rng(1, 4) 
>>> next(random_gen) 
2 
>>> next(random_gen) 
3 
>>> next(random_gen) 
2 
>>> next(random_gen) 
2 
>>> next(random_gen) 
1 
1

不知道爲什麼你需要sides變量,所以我離開它出。 您想要在每個循環中滾動模具,這意味着您必須在每個循環中將die1die2重新分配爲隨機數。

import random 

# Initial parameters 
die1 = random.randint(1,4) 
die2 = random.randint(1,4) 
count = 1 

# Loop and roll die each time 
while die1 != die2: 
    print(count, ". die number 1 is", die1, "and die number 2 is", die2,".") 
    die1 = random.randint(1,4) 
    die2 = random.randint(1,4) 
    count += 1 

# Print on which die roll you got two equal die numbers rolled 
print ("You got snake eyes! Finally! On try number", count,".") 
0

您可以使用一個for循環與iter吐出來對隨機數,enumerate會做計數,對蛇的眼睛,你也應該檢查這兩者都是1不是一個隨機匹配對:

from random import randint                 

def repeating_rand(i, j):                  
    for count, (r1, r2) in enumerate(iter(lambda: (randint(i, j), randint(i, j)), None), 1): 
     if r1 == 1 and r2 == 1:                
      return "You got snake eyes! Finally! On try number {}.".format(count)    
     print("Try no. {}, die number 1 is {} and die number 2 is {}".format(count, r1, r2)) 

輸出:

In [12]: repeating_rand(1, 4) 
Try no. 1, die number 1 is 1 and die number 2 is 2 
Try no. 2, die number 1 is 4 and die number 2 is 1 
Try no. 3, die number 1 is 1 and die number 2 is 2 
Try no. 4, die number 1 is 1 and die number 2 is 3 
Try no. 5, die number 1 is 1 and die number 2 is 3 
Try no. 6, die number 1 is 3 and die number 2 is 4 
Try no. 7, die number 1 is 4 and die number 2 is 2 
Try no. 8, die number 1 is 1 and die number 2 is 2 
Try no. 9, die number 1 is 3 and die number 2 is 2 
Try no. 10, die number 1 is 4 and die number 2 is 3 
Try no. 11, die number 1 is 1 and die number 2 is 3 
Try no. 12, die number 1 is 3 and die number 2 is 4 
Out[12]: 'You got snake eyes! Finally! On try number 13.'