Print Text 'Loading' with dots going forward and backward in python shell

后端 未结 4 1064
深忆病人
深忆病人 2021-01-13 07:01

I want to print Text \'Loading...\' But its dots would be moving back and forward (in shell).

I am creating a text game and for that it will look b

4条回答
  •  滥情空心
    2021-01-13 07:27

    A bit late but for anyone else its not that complicated.

    import os, time                                 #import os and time
    def loading():                                  #make a function called loading
        spaces = 0                                      #making a variable to store the amount of spaces between the start and the "."
        while True:                                     #infinite loop
            print("\b "*spaces+".", end="", flush=True) #we are deleting however many spaces and making them " " then printing "."
            spaces = spaces+1                           #adding a space after each print
            time.sleep(0.2)                             #waiting 0.2 secconds before proceeding
            if (spaces>5):                              #if there are more than 5 spaces after adding one so meaning 5 spaces (if that makes sense)
                print("\b \b"*spaces, end="")           #delete the line
                spaces = 0                              #set the spaces back to 0
    
    loading()                                       #call the function
    

提交回复
热议问题