简体   繁体   English

子手用户输入验证

[英]Hangman user input validation

I'm working on a Hangman game in Python and I need user validation for the input, which I have made an attempt at, but I don't know why its not working. 我正在使用Python制作的Hangman游戏,我需要对用户输入进行验证,但是我不知道为什么它不起作用。

My task was to have "error" messages for 1. empty input, 2. non-integer, non-empty input, 3. index out of range input. 我的任务是为1.空输入,2。非整数,非空输入,3。索引超出范围输入提供“错误”消息。 By index out of range, I mean that I am asking the user for an integer from 0-9 to select a word from the list already in the program. 所谓索引超出范围,是指我要用户输入一个0到9之间的整数,以便从程序中已经存在的列表中选择一个单词。

def getLetterFromUser(totalGuesses):

  while True:
      userInput = input("\nPlease enter the letter you guess:")
      if userInput == '' or userInput == ' ':
          print("Empty input.")
      elif userInput in totalGuesses:
          print("You have already guessed that letter. Try again.")
      elif userInput not in 'abcdefghijklmnopqrstuvwxyz':
          print("You must enter an alphabetic character.")
      else:
          return userInput

For clarity's sake, the subsequent call to getLetterFromUser is in a while loop so that it repeatedly checks these conditions. 为了清楚起见,随后对getLetterFromUser的调用处于while循环中,以便它反复检查这些条件。

EDIT: I took out what didn't belong. 编辑:我拿出不属于的东西。 Thank you. 谢谢。 My problem, however, is that it still tells me the input isn't in alphabet, when it is. 但是,我的问题是,它仍然告诉我输入不是字母,而是字母。 And the length of the input(a single char) is 2, which doesn't make sense unless it counts the null character. 输入的长度(单个字符)为2,除非它计算空字符,否则没有意义。

your problem is that some validation rules should take precedence over others. 您的问题是某些验证规则应优先于其他规则。 for example, if userInput is an empty string, what do you expect userInput < 0 to return ? 例如,如果userInput是一个空字符串,您期望userInput < 0返回什么? what if it's not empty but also not a number ? 如果它不为空但也不为数字怎么办?

think about which conditions should be checked first. 考虑首先应该检查哪些条件。 some functions you might want to read about and use: 您可能需要阅读和使用的一些功能:

"123".isdigit() # checks if a string represents an integer number
" 123 ".strip() # removes whitespaces at the beginning and end.
len("") # returns the length of a string
int("123") # converts a string to an int

Here are two things to start with: 从两件事开始:

what is the purpose of the line 该行的目的是什么

userInput = userInput.lower()

If you are assuming the userInput is an integer.. You should try userInput=int(userInput). 如果您假设userInput是整数。则应尝试使用userInput = int(userInput)。 Integers have no .lower() method. 整数没有.lower()方法。

the next line 下一行

if 0 > userInput or userInput > 9

this assumes userInput is an integer (you are comparing to 0 and 9, not "0" and "9") 这假设userInput是一个整数(您要与0和9进行比较,而不是“ 0”和“ 9”)

the following looks nicer: 以下看起来更好:

if not 0<=userInput<=9

You say you want integer answers, but you are not casting the input to an int, but then you say that if the input is not in the alphabet, it should return an error message. 您说您想要整数答案,但是您没有将输入转换为整数,但是您说如果输入不在字母中,则它应该返回错误消息。 You're asking for two different things. 您要提供两种不同的东西。

Do you want the user to enter an integer or a character? 您要用户输入整数还是字符?

This may help you: 这可以帮助您:

>>> int(" 33   \n")
33
>>> int(" 33a asfd")
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '33a asfd'
>>> try:
...     int("adsf")
... except ValueError:
...     print "invalid input is not a number"
...     
invalid input is not a number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM