如何忽略输入中的特殊字符和数字?

How do I ignore special chars and numbers from my input?

我的目标是在通过 input() 输入时不允许注册数字。目前允许所有字符(包括空格和特殊字符)。如何防止以下注册和显示号码?

# Keep asking the player until all letters are guessed
  while display != wordChosen:
    guess = input(str("Please enter a guess for the {} ".format(len(display)) + "letter word: "))[0:1]
    guess = guess.lower()
    #Add the players guess to the list of used letters
    used.extend(guess)
    print("Attempts: ")
    print(attempts)

# Search through the letters in answer
    for i in range(len(wordChosen)):
      if wordChosen[i] == guess:
        display = display[0:i] + guess + display[i+1:]

    print("Used letters: ")
    print(used)
        
    # Print the string with guessed letters (with spaces in between))
    print(" ".join(display))

我试过在 print(" ".join(display)) 中使用 i for i in display if not i.isdigit() 如下,但这并没有解决问题,除非我实施不正确:

# Print the string with guessed letters (with spaces in between))
    print(" ".join(i for i in display if not i.isdigit()))

终端示例的当前输出,我不希望数字被确认或显示为已使用的猜测: Used letters: ['d', '4', '5', '6', '7', '8', '9']

您选择的变量命名方式有点难以理解(不清楚display应该是什么)

但是你似乎走在了正确的轨道上,你需要做的只是在你得到输入之后,在你扩展列表之前,检查输入是否有效,如果有效,将它添加到列表中,否则告诉他们它是无效的并且 continue 要求新的输入:

guess = input(...)
if guess.isdigit():
    # throw an error, or just tell the user the input is invalid.
    continue
used.extend(guess.lower())
...