TypeError:time.sleep需要一个整数(类型为NoneType)。

时间:2018-11-12 14:47:18

标签: python

我一直在尝试这段代码来获取随机种子,但是它总是失败。

    import string
import random
import time
import sys
from random import seed
possibleCharacters = string.ascii_lowercase + string.digits + string.ascii_uppercase + ' .,!?;:'

target = input("Enter text: ")
attemptThis = ''.join(random.choice(possibleCharacters) for i in range(len(target)))
attemptNext = ''

completed = False

generation = 0

while completed == False:
    print(attemptThis)
    attemptNext = ''
    completed = True
    for i in range(len(target)):
        if attemptThis[i] != target[i]:
            completed = False
            attemptNext += random.choice(possibleCharacters)
        else:
            attemptNext += target[i]
    generation += 1
    attemptThis = attemptNext
    time.sleep(0)

time.sleep(seed(1))    

print("Operation completed in " + str(generation) + " different generations!")

错误总是这样:

Traceback (most recent call last):
  File "N:\ict python\python programs\demo\genration text.py", line 30, in <module>
    time.sleep(seed(1))
TypeError: an integer is required (got type NoneType) at the end of it.

我尝试了其他功能-randint,pi,并生成一个随机数并由随机数决定。

我必须对该数字进行硬编码,还是有办法使其产生随机延迟?

3 个答案:

答案 0 :(得分:3)

random.seed()函数始终返回None,但是time.sleep()需要一个数字(睡眠秒数)。

尝试使用random.randint()生成一个随机整数:

import random
time.sleep(random.randint(1, 50))  

答案 1 :(得分:1)

还使用random.randint(a, b)函数-生成随机整数。 seed函数只是初始化随机数生成器。

答案 2 :(得分:0)

种子的类型为None,并且time.sleep()需要一个整数。 要发送随机整数作为睡眠参数,您可以尝试:

time.sleep(random.randint(1, 60)) 

您需要为此导入randim libraby。