Python 如何使用print(chr(27)&“x2B”[2J”)?当我使用print()函数时,input()函数工作不正常。谢谢

Python 如何使用print(chr(27)&“x2B”[2J”)?当我使用print()函数时,input()函数工作不正常。谢谢,python,input,terminal,chr,Python,Input,Terminal,Chr,我正在用Python制作HangMan游戏。它几乎已经准备好了,但当我每次在循环运行时使用print(chr(27)+“[2J”)清除控制台时,它会清除,但我的guess=input(“猜字母”)不起作用。当我把它评论出来时,它又完美地工作了。我不明白问题是什么。我搜索了我的问题的答案,但我找不到。如果有人解释一下这个打印(chr(27)+“[2J”)是如何工作的,我将不胜感激。下面是我的代码: #columns 0 1 2 3 4 5 6 7 8 9

我正在用Python制作HangMan游戏。它几乎已经准备好了,但当我每次在循环运行时使用print(chr(27)+“[2J”)清除控制台时,它会清除,但我的guess=input(“猜字母”)不起作用。当我把它评论出来时,它又完美地工作了。我不明白问题是什么。我搜索了我的问题的答案,但我找不到。如果有人解释一下这个打印(chr(27)+“[2J”)是如何工作的,我将不胜感激。下面是我的代码:

   #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")
#列01 2 3 4 5 6 7 8 9 10 11
gallowList=[#行
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,1
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,2
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,3
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,4
]          
def加仑(listVar):
打印(“\\\\\\\”+“\n”+
"   |     |  "          )
对于listVar中的行:
对于行中的列:
打印(col,end=“”)
打印(“____124;_”+“\n”+
"|          |"        )    
单词=输入(“思考一个单词:”)
而word.isspace()或len(word)==0:
word=输入(“思考一个有效的单词:”)
打印(chr(27)+“[2J”)#在这里,我应该在播放器之后清除屏幕
#漏嘴
rightGuesses=[]
错误猜测=[]
对于大写字母:
如果字母!=“”:
rightGuesses.append(“\uquot”)
其他:
右猜测。追加(“”)
尽管如此:
绞刑架(绞刑架)
打印(“您正在猜测的单词:”)
对于RightGuess中的字母:
如果字母==“”:
打印(字母,end=“”)
其他:
打印(字母+“”,结束=”)
打印(“\n错误猜测:”,错误猜测)
如果列表(单词)==右猜测:
打印(“猜玩家赢了!”)
打破
elif len(错误猜测)=9:
打印(“悬挂玩家赢了!”)
打破
猜测=输入(“使用字母:”)
而guess.isspace()或len(guess)!=1:
猜测=输入(“使用有效字母:”)
numocurinword=单词数(猜测)#2
numOccurInRightGuesses=rightGuesses.count(guess)#0
#你好02
如果numOccurInRightGuesses
如果这是Windows,那么
[2J
序列早就有问题了(尽管它可能已经被修复)。请尝试
[H[J
。另外,我想您可以在字符串中编码
ESC
,而不是使用
chr()
:尝试
“\x1b[H\x1b[J”
while
循环之所以运行,是因为您处于
while
循环中,并且从未告诉它停止。您需要在函数中获取
while
,这样您就可以中断该循环,然后返回该循环。谢谢您的回答。