[英]Can I make a Python time.sleep() with a variable?
我创建了一个 function,它在终端 window 中制作了一个加载条。 它看起来像这样:
def loadingBar(length, time):
void = '-'
fill = '#'
count = 100/length
increaseCount = 0
sleepTime = time/20
for i in range(length):
print('['+(fill*i)+(void*(length-i))+'] '+str(int(increaseCount))+'%',end='\r')
increaseCount += count
time.sleep(sleepTime)
print('['+(fill*(i+1))+(void*(length-(i+1)))+'] '+str(int(increaseCount))+'%',end='\n')
我想用变量“sleepTime”自定义睡眠时间,但我有一个错误说:
AttributeError: 'float' object has no attribute 'sleep'
我不明白,因为变量“时间”和变量“睡眠时间”是浮动的!
注意:我的英语不是很好。
目前,参数time
与模块时间同名。
改变:
def loadingBar(length, time):
至:
def loadingBar(length, time1):
和
sleepTime = time/20
至
sleepTime = time1/20
这将起作用:
def loadingBar(length, time1):
void = '-'
fill = '#'
count = 100/length
increaseCount = 0
sleepTime = time1/20
for i in range(length):
print('['+(fill*i)+(void*(length-i))+'] '+str(int(increaseCount))+'%',end='\r')
increaseCount += count
time.sleep(sleepTime)
print('['+(fill*(i+1))+(void*(length-(i+1)))+'] '+str(int(increaseCount))+'%',end='\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.