I am making HangMan game in Python. It is almost ready but when I use print(chr(27) + "[2J") to clear the console every time while loop runs, it clears but then my guess = input("Guess a letter") is not working. And when I comment it out it works perfectly again. I don't understand what the problem is. I searched to find an answer to my problem, but I couldn't. And I would appreciate if somebody explained how this print(chr(27) + "[2J") works. Below is my code:
#columns 0 1 2 3 4 5 6 7 8 9 10 11
gallowList = [ #rows
[" "," "," "," "," "," "," "," "," ","|"," "," \n"], #0
[" "," "," "," "," "," "," "," "," ","|"," "," \n"], #1
[" "," "," "," "," "," "," "," "," ","|"," "," \n"], #2
[" "," "," "," "," "," "," "," "," ","|"," "," \n"], #3
[" "," "," "," "," "," "," "," "," ","|"," "," \n"], #4
]
def gallow(listVar):
print( " _____ " + "\n" +
" | | " )
for row in listVar:
for col in row:
print(col, end = "")
print( " ________|_ " + "\n" +
"| |" )
word = input("Think of a word: ")
while word.isspace() or len(word) == 0:
word = input("Think of a valid word: ")
print(chr(27) + "[2J") # here I should clear the screen after the player
# neters a word
rightGuesses = []
wrongGuesses = []
for letter in word:
if letter != " ":
rightGuesses.append("_")
else:
rightGuesses.append(" ")
while True:
gallow(gallowList)
print("Word you are guessing:")
for letter in rightGuesses:
if letter == " ":
print(letter, end = "")
else:
print(letter +".", end = "")
print("\nWrong guesses:", wrongGuesses)
if list(word) == rightGuesses:
print("Guessing Player has won!")
break
elif len(wrongGuesses) == 9:
print("Hanging player has won!")
break
guess = input("Geuss a letter: ")
while guess.isspace() or len(guess) != 1:
guess = input("Geuss a valid letter: ")
numOccurInWord = word.count(guess) # 2
numOccurInRightGuesses = rightGuesses.count(guess) #0
# hello 0 2
if numOccurInRightGuesses < numOccurInWord:
for index in range(len(word)): # hello
if word[index] == guess: # l == l
if rightGuesses[index] != guess:
rightGuesses[index] = guess
break
else:
wrongGuesses.append(guess)
if len(wrongGuesses) == 1:
gallowList[0][3] = "O"
elif len(wrongGuesses) == 2:
gallowList[1][2] = "/"
elif len(wrongGuesses) == 3:
gallowList[1][3] = "|"
elif len(wrongGuesses) == 4:
gallowList[1][4] = "\\"
elif len(wrongGuesses) == 5:
gallowList[2][3] = "O"
elif len(wrongGuesses) == 6:
gallowList[3][2] = "/"
elif len(wrongGuesses) == 7:
gallowList[3][4] = "\\"
elif len(wrongGuesses) == 8:
gallowList[4][1] = "/"
elif len(wrongGuesses) == 9:
gallowList[4][5] = "\\"
# here every time while loop runs, I want to clear the screen and redraw
# everything
print(chr(27) + "[2J")