影影影影影少丶 发表于 2020-2-23 00:30:16

Python运行问题

有没有大佬解答一下,为什么同一串代码,点击run module以后,有时候能得到结果,有时候不能呢。

冬雪雪冬 发表于 2020-2-23 10:11:24

发代码

影影影影影少丶 发表于 2020-2-23 11:22:46

冬雪雪冬 发表于 2020-2-23 10:11
发代码

import random
n = 8
red = yellow = green = 0
while n:
    color = random.choice(['red','green','yellow'])
    if color == 'red':
      red += 1
    elif color == 'green':
      green += 1
    else :
      yellow += 1
    if red > 3 or yellow > 3 or green > 6:
      continue
    n -= 1
print('red*',red,'+yellow*',yellow,'+green*',green)

冬雪雪冬 发表于 2020-2-23 11:29:45

如果red > 3yellow > 3   green > 6有任何一个成立,则continue,n不会再变化,形成无限循环

影影影影影少丶 发表于 2020-2-23 14:27:32

冬雪雪冬 发表于 2020-2-23 11:29
如果red > 3yellow > 3   green > 6有任何一个成立,则continue,n不会再变化,形成无限循环

好的,谢谢。明白了,多谢解答
页: [1]
查看完整版本: Python运行问题