-2

I've created a function who makes a loading bar in the terminal 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 :

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.

bhristov
  • 3,137
  • 2
  • 10
  • 26
  • When you run `time.sleep`, the word `time` should indicate [the time module](https://docs.python.org/3/library/time.html). But in your code you have created a float variable with the same name. Change the name of the variable to something else. – khelwood Jun 18 '20 at 08:53
  • are you looking for something like [this](https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console)? – FObersteiner Jun 18 '20 at 08:54

1 Answers1

0

Currently, the parameter time has the same name as the module 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')
bhristov
  • 3,137
  • 2
  • 10
  • 26
  • @RandomProgrammer no problem. I am glad that I could help. If my answer helped you please mark it as the answer to your question, also please consider upvoting. – bhristov Jun 18 '20 at 09:00