TypeError:需要整数(get type NoneType)时间。睡觉

2024-09-20 22:54:10 发布

您现在位置: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!")

错误总是这样:

^{pr2}$

我试过其他函数——randint,pi,生成一个随机数,然后用一个随机数来决定。在

我是必须对这个数字进行硬编码,还是有办法让它产生随机延迟?在


Tags: inimportfalsetargetstringtimeasciirandom
3条回答

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

种子的类型是“无”和时间。睡觉()需要整数。 要发送一个随机整数作为睡眠参数,您可以尝试:

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

你需要为此导入randim libraby。在

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

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

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

相关问题 更多 >

    热门问题