(Python) Why is my while loop not working?

Question:

I have been working on an operating system inside of Python and I am starting off with the system booting code. I have a while loop that executes when the variable "Booting" = 1. Inside the while loop is a script that prints "Booting" then replaces that with "Booting.", then it gets replaced with "Booting.." and so on until it reaches "Booting….." which should make it reset to "Booting" and reset the cycle. Instead, it just stops at "Booting….." and doesn’t continue to reset the cycle.

Here is the code:

import time
import sys

Booting = 1

while Booting == 1:
 print("Booting")
 sys.stdout.write("33[F")
 time.sleep(0.2)
 print("Booting.")
 sys.stdout.write("33[F")
 time.sleep(0.2)
 print("Booting..")
 sys.stdout.write("33[F")
 time.sleep(0.2)
 print("Booting...")
 sys.stdout.write("33[F")
 time.sleep(0.2)
 print("Booting....")
 sys.stdout.write("33[F")
 time.sleep(0.2)
 print("Booting.....")
 sys.stdout.write("33[F")
 time.sleep(0.2)
Asked By: Jack Arsenfield

||

Answers:

it does not erase the rest of the line, so you need to replace any existing variables you want to overwrite with spaces

try

...
print("booting    ")
...
print("booting.   ")
...

etc

(there are many ways to clear the line this is just one (@code provides another good alternative in the comments)

Answered By: Joran Beasley
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.