这是我的第一个问题,所以要温柔点。
我正在制作我的第一个python项目,并在学习过程中生成了以下代码:
import random
count = random.randint(1,10)
print (count) ;
while (count < 9):
print ('The count is:', count)
count = random.randint(1,10)
print (count)
print ("Good bye!")标签有点奇怪,因为我改编了一个基本的教学作品,运行时的输出显然是计数是: 1-8之间的数字,重复到你得到9,然后它停止,我的问题是,在随机数为== 9之前,有没有办法计算循环的数量?
发布于 2018-06-08 23:51:23
我建议你两种可能的解决方案:
根据@Igle在评论中的建议,
导入随机计数= random.randint(1,10) print ( count ) nbOfCycles =0 while (count < 9):print ('The count is:',count) nbOfCycles += 1 count = random.randint(1,10) print (count,"after",nbOfCycles,"cycles") print (“再见!”)
https://stackoverflow.com/questions/50760872
复制相似问题