Python在多个条件下都是随机的

Python在多个条件下都是随机的,python,random,multiple-conditions,Python,Random,Multiple Conditions,干杯,我的问题是我不知道如何在多种情况下做一段时间。我真的不明白为什么这行不通: import random a = 0 b = 0 c = 0 while a < 190 and b < 140 and c < 110: # <-- This condition here a = 0 b = 0 c = 0 for i in range(1, 465): v = random.randint(1, 3)

干杯,我的问题是我不知道如何在多种情况下做一段时间。我真的不明白为什么这行不通:

import random

a = 0
b = 0
c = 0

while a < 190 and b < 140 and c < 110: # <-- This condition here
    a = 0
    b = 0
    c = 0

    for i in range(1, 465):
        v = random.randint(1, 3)

        if v == 1:
            a = a + 1
        elif v == 2:
            b = b + 1
        else:
            c = c + 1

    result = ""
    result += "a: " + str(a) + "\n"
    result += "b: " + str(b) + "\n"
    result += "c: " + str(c) + "\n"

    print (result)
随机导入
a=0
b=0
c=0

当a<190、b<140和c<110时:#可以在逻辑上稍微更改,并使用无限循环,然后在满足条件时将其打破:

while True:
    # do stuff
    if a >= 190 and b >= 140 and c >=110:
        break
如果满足任何条件,则原始逻辑终止。例如,此循环退出是因为在第一次迭代后,
a
不再是
True

a = True 
b = True
while a and b:
    a = False
这个循环是无限的,因为
b
总是
True

a = True
b = True
while a or b:
    a = False

您可以使用
而不是
作为初始
while
循环,但我发现
中断逻辑更加直观。

您可以稍微更改逻辑,使用无限循环,然后在满足条件时将其中断:

while True:
    # do stuff
    if a >= 190 and b >= 140 and c >=110:
        break
如果满足任何条件,则原始逻辑终止。例如,此循环退出是因为在第一次迭代后,
a
不再是
True

a = True 
b = True
while a and b:
    a = False
这个循环是无限的,因为
b
总是
True

a = True
b = True
while a or b:
    a = False

您可以使用
而不是
作为初始
while
循环,但我发现
中断
逻辑更加直观。

您正在重置循环体中的
a
b
c
。 试试这个:

>>> count = 0
>>> while a < 190 and b < 140 and c < 110 and count < 10: # <-- This condition here
...   count += 1
...   a = 0
...   b = 0
...   c = 0
...   print(count, a, b, c)
... 
(1, 0, 0, 0)
(2, 0, 0, 0)
(3, 0, 0, 0)
(4, 0, 0, 0)
(5, 0, 0, 0)
(6, 0, 0, 0)
(7, 0, 0, 0)
(8, 0, 0, 0)
(9, 0, 0, 0)
(10, 0, 0, 0)
>>> 
>计数=0
>>>而a<190,b<140,c<110,计数<10:

您正在重置循环体中的
a
b
c
。 试试这个:

>>> count = 0
>>> while a < 190 and b < 140 and c < 110 and count < 10: # <-- This condition here
...   count += 1
...   a = 0
...   b = 0
...   c = 0
...   print(count, a, b, c)
... 
(1, 0, 0, 0)
(2, 0, 0, 0)
(3, 0, 0, 0)
(4, 0, 0, 0)
(5, 0, 0, 0)
(6, 0, 0, 0)
(7, 0, 0, 0)
(8, 0, 0, 0)
(9, 0, 0, 0)
(10, 0, 0, 0)
>>> 
>计数=0
>>>而a<190,b<140,c<110,计数<10:

实际上,当while循环迭代时,您只显示“a,b,c”,并且在每个循环中递增465次。这意味着如果你的while循环工作4倍,它将随机增加a,b,c 465*4倍。而你的值对于这种增量来说太小了。作为一个解决方案,您可以减少465个数字,如果将其设置为250,您将看到它将一直工作,直到c达到110以上并完成迭代

for i in range(1, 250):
    v = random.randint(1, 3)
温度为250℃时,达到114并完成迭代。这是因为250/3~=83。当数字随机分配时,c最常达到极限。我想你想要这样的东西

import random

a = 0
b = 0
c = 0

while a < 190 and b < 140 and c < 110: # <-- This condition here
    v = random.randint(1, 3)
    if v == 1:
        a = a + 1
    elif v == 2:
        b = b + 1
    else:
        c = c + 1

    result = ""
    result += "a: " + str(a) + "\n"
    result += "b: " + str(b) + "\n"
    result += "c: " + str(c) + "\n"

    print (result)
随机导入
a=0
b=0
c=0

当a<190、b<140和c<110时:#实际上,当while循环迭代并且在每个循环中递增465次时,您只显示“a、b、c”。这意味着如果你的while循环工作4倍,它将随机增加a,b,c 465*4倍。而你的值对于这种增量来说太小了。作为一个解决方案,您可以减少465个数字,如果将其设置为250,您将看到它将一直工作,直到c达到110以上并完成迭代

for i in range(1, 250):
    v = random.randint(1, 3)
温度为250℃时,达到114并完成迭代。这是因为250/3~=83。当数字随机分配时,c最常达到极限。我想你想要这样的东西

import random

a = 0
b = 0
c = 0

while a < 190 and b < 140 and c < 110: # <-- This condition here
    v = random.randint(1, 3)
    if v == 1:
        a = a + 1
    elif v == 2:
        b = b + 1
    else:
        c = c + 1

    result = ""
    result += "a: " + str(a) + "\n"
    result += "b: " + str(b) + "\n"
    result += "c: " + str(c) + "\n"

    print (result)
随机导入
a=0
b=0
c=0

当a<190、b<140和c<110时:#我希望每次都以0开始算法,所以不可能得到超过465的总数。这就是我为什么这么做的原因。顺便说一句,这个答案做到了。你能告诉我为什么while条件不起作用吗?@PatrickMlr如果满足这些条件,你的初始
while
循环将退出。例如,如果
a=191
,则循环将终止,并使用python或?这很奇怪。我希望每次都以0开始算法,所以不可能得到超过465的总数。这就是我为什么这么做的原因。顺便说一句,这个答案做到了。你能告诉我为什么while条件不起作用吗?@PatrickMlr如果满足这些条件,你的初始
while
循环将退出。例如,如果
a=191
,则循环将终止,并使用python或?真奇怪。