Python 为什么不是';我的程序没有注意到字母之间的等价性吗?
我正在用Python 2制作一个简单的刽子手游戏。到目前为止,我的代码是我为它所做的基础工作,但它似乎不起作用。如果能给我一个简单的提醒,告诉我哪些代码不起作用,我会很感激的 代码: 输出:Python 为什么不是';我的程序没有注意到字母之间的等价性吗?,python,python-2.x,Python,Python 2.x,我正在用Python 2制作一个简单的刽子手游戏。到目前为止,我的代码是我为它所做的基础工作,但它似乎不起作用。如果能给我一个简单的提醒,告诉我哪些代码不起作用,我会很感激的 代码: 输出: get\u guess是一个函数,您需要在它后面加上()来调用该函数 您不应该将对input()的调用作为默认参数。默认值在定义函数时计算一次,而不是每次调用函数时。您应该在函数中指定guess 您应该在单个循环中测试所有无效输入 def get_guess(): while True:
get\u guess
是一个函数,您需要在它后面加上()
来调用该函数
您不应该将对input()
的调用作为默认参数。默认值在定义函数时计算一次,而不是每次调用函数时。您应该在函数中指定guess
您应该在单个循环中测试所有无效输入
def get_guess():
while True:
guess = input("Guess:")
if len(guess) != 1:
print "Your guess must be exactly one character!"
continue
if not guess.isalpha() or not guess.islower():
print "Your guess must be a lowercase letter!"
continue
break
return guess
while True:
guess = get_guess()
if guess in secret_word_list:
print "That letter is in the secret word!"
else:
print "That letter is not in the secret word!"
这里有几个问题,但最大的问题是你没有调用函数,所以你将函数本身与秘密进行比较 带有修复程序的代码:
secret_word = 'tracy' # Don't make secret_word_list, there's no point; just use the str itself since you only test len 1 strings against it anyway
print secret_word
def get_guess(guess): # Don't make the default call input, that'll prompt once for an input and store it as the permanent default
while True:
# Test each condition and break loop only if all past; original code would never
# recheck length if new value entered after testing isalpha
if len(guess) != 1:
print "Your guess must be exactly one character!"
elif not guess.islower(): # Add missing call parens on islower; use not, never compare to False; islower implicitly verifies isalpha, so avoid testing isalpha
print "Your guess must be a lowercase letter!"
else:
break # Passed all tests, break loop
# Get new guess if any test failed
guess = raw_input("Guess: ") # Use raw_input on Python 2, never input (which eval's the result of raw_input)
# Removed else (loop always ends by breaking, using else nonsensical but harmless in original code too
return guess
while True:
# Move guess getting to if, because having it in else case never actually checked it
if get_guess(raw_input("Guess: ")) in secret_word:
print "That letter is in the secret word!"
else:
print "That letter is not in the secret word!"
注意:我保持了一种奇怪的行为,即让
get\u guess
接受一个参数,但在失败时重新进行猜测。一个更明智的解决方案是完全删除guess
参数,并将guess=raw\u输入(“guess:”)
移动到循环的顶部(删除末尾的else
块)。您不需要秘密单词列表。您可以简单地使用secret\u word
本身进行“in”检查,例如如果't'in secret\u word:
您的固定代码与我的非常相似。:-)次要更正:
不是Python中的运算符;你需要拼出而不是。另外,str.islower
隐式要求至少一个大小写字符,并且由于已知长度为1,因此根本不需要对isalpha
进行测试。这非常有用!非常感谢你花时间来指导我,我是一个新手,所以我不知道其中的利基(比如从不与虚假进行比较,以及原始输入),但我显然也需要更新一些更基本的原则。函数接受一个参数的原因是赋值要求它接受一个参数,我认为这效率很低,但实际上对此无能为力。再次感谢您给出的令人惊讶的答案。@robs:好吧,如果您是新手,我可以建议您学习Python 3吗?Python2在几年前就停止提供新功能,并将在今年年底完全停止支持。除其他外,它使input
相当于Python2的raw_input
,因此您在Python3上选择的API应该是正确的。:-)我很想切换,但是目前我正在学习的课程要求在Python2中完成。不知道为什么。这是考试的最后一项作业,所以我希望我能完成这项任务,然后继续学习python 3。再次感谢:)
secret_word = 'tracy' # Don't make secret_word_list, there's no point; just use the str itself since you only test len 1 strings against it anyway
print secret_word
def get_guess(guess): # Don't make the default call input, that'll prompt once for an input and store it as the permanent default
while True:
# Test each condition and break loop only if all past; original code would never
# recheck length if new value entered after testing isalpha
if len(guess) != 1:
print "Your guess must be exactly one character!"
elif not guess.islower(): # Add missing call parens on islower; use not, never compare to False; islower implicitly verifies isalpha, so avoid testing isalpha
print "Your guess must be a lowercase letter!"
else:
break # Passed all tests, break loop
# Get new guess if any test failed
guess = raw_input("Guess: ") # Use raw_input on Python 2, never input (which eval's the result of raw_input)
# Removed else (loop always ends by breaking, using else nonsensical but harmless in original code too
return guess
while True:
# Move guess getting to if, because having it in else case never actually checked it
if get_guess(raw_input("Guess: ")) in secret_word:
print "That letter is in the secret word!"
else:
print "That letter is not in the secret word!"