随机增加数

时间:2020-04-19 16:16:33

标签: python

我正在尝试制作一个函数(或只是一段代码),其计数范围从0到100,而程序将打印的每个数字都大于上一个(例如:0,14,20 ,21,26,34,58,.. 100)。我所做的只是一个将每1秒钟将数字增加1的函数。

    import time,random
def inumber(count):
    while count!=101:
          time.sleep(1.0)
          (random.randrange(0, 100))
          count=count+1;
          print(count)
inumber(0)

2 个答案:

答案 0 :(得分:1)

您不想在每次迭代中获得0到100之间的随机数,而是希望将计数器调整为当前数字+1和101之间的随机数(randrange()的限制是期望最大数量+ 1)。

import time
import random


def inumber(count):
    print(count)  # Print first number
    while count < 100:
        time.sleep(1.0)
        # Set count to a number between the next number and 101
        count = random.randrange(count+1, 101)
        print(count)


inumber(0)

答案 1 :(得分:0)

iPad