如何使用打印chr27+“[2J”?当我使用这个 print() 函数时 input() 函数不能正常工作谢谢

问题描述

我正在用 Python 制作 HangMan 游戏。它几乎准备好了,但是当我使用 print(chr(27) + "[2J") 每次循环运行时清除控制台时,它会清除,但随后我的 guess = input("猜字母") 不起作用。当我将其注释掉时,它再次完美运行。我不明白问题是什么。我试图找到我的问题的答案,但我找不到。如果有人解释了这个 print(chr(27) + "[2J") 是如何工作的,我将不胜感激。下面是我的代码

   #columns  0   1   2   3   4   5   6   7   8   9  10  11                                  
gallowList = [                                                   #rows
          [" "," ","|"," \n"],#0
          [" ",#1
          [" ",#2
          [" ",#3
          [" ",#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")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)