简体   繁体   English

我可以使用变量制作 Python time.sleep() 吗?

[英]Can I make a Python time.sleep() with a variable?

I've created a function who makes a loading bar in the terminal window.我创建了一个 function,它在终端 window 中制作了一个加载条。 It looks like this:它看起来像这样:

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')

I want to customize the sleep time with the variable “sleepTime”, but I have an error who says:我想用变量“sleepTime”自定义睡眠时间,但我有一个错误说:

AttributeError: 'float' object has no attribute 'sleep'

I don't understand because the variable “time” and the variable “sleepTime” are float!我不明白,因为变量“时间”和变量“睡眠时间”是浮动的!
Note: I'm not very good at English.注意:我的英语不是很好。

Currently, the parameter time has the same name as the module time.目前,参数time与模块时间同名。

Change:改变:

def loadingBar(length, time):

To:至:

def loadingBar(length, time1):

and

sleepTime = time/20

to

sleepTime = time1/20

This will work:这将起作用:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM