Python 如何检查输入值是否为有效字符串?

Python 如何检查输入值是否为有效字符串?,python,Python,所以我在做一个《刽子手》的游戏,首先是一个“僵尸大师”输入一个字符串,然后是玩家要猜多少个字符串。我刚刚开始,我正在尝试编写一个函数来检查BotMaster输入的内容是否是有效字符串。有效字符串应该是只包含字母、无符号、数字或额外空格的字符串。我已经有了删除额外空格和非alpha输入的函数(因此它会删除句点、额外空格等)和一个使其全部小写的函数,但是如果我输入一个数字或一个空字符串,我的函数就会中断。我应该如何添加这些 #Imports (this is for later code I hav

所以我在做一个《刽子手》的游戏,首先是一个“僵尸大师”输入一个字符串,然后是玩家要猜多少个字符串。我刚刚开始,我正在尝试编写一个函数来检查BotMaster输入的内容是否是有效字符串。有效字符串应该是只包含字母、无符号、数字或额外空格的字符串。我已经有了删除额外空格和非alpha输入的函数(因此它会删除句点、额外空格等)和一个使其全部小写的函数,但是如果我输入一个数字或一个空字符串,我的函数就会中断。我应该如何添加这些

#Imports (this is for later code I haven't written)
import os,time,random

#Removes extra spaces from the function
def space_cull(the_str):
  result = the_str 
  result = result.strip()
  result =" ".join(result.split())
  the_str = result
  return the_str

#Makes the string lowercase
def make_lower(the_str):
  the_str = the_str.lower()
  return the_str

#Checks if everything in the string are Alpha Inputs
def check_alpha(the_str):
  the_str =''.join([char for char in the_str if char.isalnum()])
  return the_str 

#Ask Botmaster the string they want
def ask_bot():
  while True:
   bot_str = input('Enter a string for the player to guess: ')
   bot_str = space_cull(bot_str)
   bot_str = make_lower(bot_str)
   bot_str = check_alpha(bot_str)
   if bot_str == '':
      print('That is not a correct string, try again')
      True
   return bot_str

ask_bot()
我添加了ask_bot()部分,以便更快地测试函数 情况就是这样:

Enter a string for the player to guess: 1
#nothing 
#Tested again:
Enter a string for the player to guess: ''
That is not a correct string, try again.
#But then exits the loop, which I don't want it to, if the string is wrong I want it to ask them again.
#Tested Again
Enter a string for the player to guess: 'Katze'
#Nothing, which is actually good this time

如何解决这个问题?

正如John Gordon已经提到的,解决方案是“str”类的方法“isalpha”

userInput=input(“您的建议:”)
如果userInput.isalpha():
#施点魔法
其他:
打印(“请仅键入字母”)

您的while循环将始终在写入函数时终止

def ask_bot():
  while True:
   bot_str = input('Enter a string for the player to guess: ')
   bot_str = space_cull(bot_str)
   bot_str = make_lower(bot_str)
   bot_str = check_alpha(bot_str)
   if bot_str == '':
      print('That is not a correct string, try again')
      True # <- this does nothing
   return bot_str # < - this breaks out of the function and the loop
正如其他答案已经提到的,您可以使用str.isalpha()检查字符串是否有效,或者如果您想修改字符串,则需要调整check_alpha函数,如下所示:

def check_alpha(the_str):
  the_str =''.join([char for char in the_str if char.isalpha()])
  return the_str 
您根本不需要check_alpha(str)函数。修改ask_bot(),如下所示


ask_bot()

s。如果字符串
s
仅包含字母,isalpha()
将返回True,否则返回False。它表示continue不在循环中正确检查编辑,现在应该可以了。我在键入if块时打错了。它本来应该在循环中。道歉没关系:)我总是犯错误。我把希尔伯特曲线称为吉尔伯特曲线,洛丽特做到了!但是我有一个问题,如果我只输入1作为输入,它会捕捉到,并说这是一个数字,然后返回它,这是好的,但是如果我输入“1”,它会让它通过。有没有办法抓住它?太好了!当然,检查编辑。添加一个额外的“continue”就可以了。此外,如果它有效,请不要投票!谢谢,但我该如何做才能确保如果它不是正确的字符串,它将再次要求Bot主机输入正确的字符串?修改后的ask_Bot()函数会这样做-通过添加else语句,函数会不断要求新字符串,直到传递正确的字符串。我想使用try和except来降低崩溃的风险,我已经写了一些代码,但它将继续到下一个函数,即使它不是alpha。
def check_alpha(the_str):
  the_str =''.join([char for char in the_str if char.isalpha()])
  return the_str 
def ask_bot():
  while True:
   bot_str = input('Enter a string for the player to guess: ')
   bot_str = space_cull(bot_str)
   bot_str = make_lower(bot_str)
   if not bot_str.isalpha():
     if bot_str.isnum():
       print("The entered string is a number")
       continue

     if bot_str == '':
       print('That is not a correct string, try again')
       continue

     continue

   break

return bot_str