-1

If I have a line that says the following:

3 guesses left

After the print statement for this I have a loop that allows the user to input something and remove one from guesses each time e.g.

guesses = 3

while guesses > 0:
    guess = input("Have a guess: ")
    guesses -= 1

After the last line of code how can I make it continue to iterate through the while loop but instead of printing extra lines that say 2 guesses left, 1 guess left, 0 guess left, simply change the number in the first line?

Weafs.py
  • 22,731
  • 9
  • 56
  • 78
Tehloltractor
  • 85
  • 1
  • 5
  • Possible duplicate: http://stackoverflow.com/questions/517127/how-do-i-write-output-in-same-place-on-the-console – jaynp Nov 21 '14 at 23:44

1 Answers1

0

you can use sys.stdout.write with \r. \r puts back to start of line:

demo:

>>> guess = 3
>>> while guess != 0:
...     sys.stdout.write("\rguess left " + str(guess-1))
...     sys.stdout.flush()
...     time.sleep(2)
...     guess = guess -1

or you can clear whole screen before next print:

print "\033c"        # python 2x
print("\033c")       # python 3x

you can also use unix and windows command:

import os
os.system('clear')  # unix
os.system('cls')    # windows
Hackaholic
  • 19,069
  • 5
  • 54
  • 72