在while循环中计数/随机int

时间:2016-02-28 16:12:35

标签: python loops random while-loop integer

我希望这个while循环可以在每次迭代时更改数字(包括count和random int。)但是当我运行程序时,循环只会继续使用相同的数字和随机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,".")

4 个答案:

答案 0 :(得分:4)

每次调用 random.randint(1,4)时,您都会创建一个随机数。每当您打印它时,它都不会神奇地更改为新的随机数。

random.randint(1, 4)循环中生成while的新随机数。

第二个问题是count == count + 1检查是否相等(并在您的情况下返回False)。要执行分配,请使用分配运算符=count += 1count增加一。

如果你想要一个无休止地吐出随机数的发生器,写一个:

>>> 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 :(得分:2)

您还需要在random循环内执行while调用,否则它们不会更改。另一件事是你比较==而不是设置=计数器:

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 .

一个几乎完全相同但在while循环中使用break代替条件的替代方案是:

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

答案 2 :(得分: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,".")

答案 3 :(得分:0)

你可以使用带有iter的for循环来吐出一对随机数,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.'