Python程序的行为不符合预期
我正在用Python编写我的第一个短程序。此程序应在第三次错误猜测后停止。程序在第一个问题上按预期工作,并在三次错误猜测后自行停止 然而,我不明白为什么在第二个问题上,在第三次错误猜测之后,第一个问题被重复Python程序的行为不符合预期,python,logic,Python,Logic,我正在用Python编写我的第一个短程序。此程序应在第三次错误猜测后停止。程序在第一个问题上按预期工作,并在三次错误猜测后自行停止 然而,我不明白为什么在第二个问题上,在第三次错误猜测之后,第一个问题被重复 easy_text = '''Hello __1__!' In __2__ this is particularly easy; all you have to do is type in: __3__ "Hello __1__!" Of course, that isn't a ver
easy_text = '''Hello __1__!' In __2__ this is particularly easy; all you
have to do is type in: __3__ "Hello __1__!" Of course, that isn't a very
useful thing to do. However, it is an example of how to output to the user
using the __3__ command, and produces a program which does something, so it
is useful in that capacity..'''
def split_string():
global easy_text_splitted
easy_text_splitted = easy_text.split(" ")
return None
def level1():
print """You will get 3 guesses per problem
The current paragraph reads as such:"""
print easy_text
return first_question(3)
def first_question(guesses):
while guesses > 0:
split_string()
first_answer = raw_input("What should be substituted in for __1__?")
if first_answer.lower() == "world":
easy_text_splitted[1] = first_answer
easy_text_splitted[19] = first_answer
new_easy_text = " ".join(easy_text_splitted)
print new_easy_text
second_question(3)
else:
guesses -= 1
print "That's not the answer I expected. You have " +
str(guesses) + " guess(es) left"
return first_question(guesses)
def second_question(guesses):
while guesses > 0:
split_string()
second_answer = raw_input("What should be substituted in for 2?")
if second_answer.lower() == "python":
easy_text_splitted[4] = second_answer
new_easy_text = " ".join(easy_text_splitted)
print new_easy_text
else:
guesses -= 1
print "That's not the answer I expected. You have \n " +
str(guesses) + " guess(es) left"
return second_question(guesses)
def level_selection(index):
level = raw_input("Choose the desired level of difficulty: easy, \n
medium, or hard ")
if level.lower() == "easy":
return level1()
if level.lower() == "medium":
return level2
if level.lower() == "hard":
return level3
else:
print "Error"
index -= 1
if index > 0:
return level_selection(index)
return "Bye!"
print level_selection(3)
level2 = "You selected medium"
level3 = "You selected hard"
调用
second\u question
时,无论guesss
的值在first\u question
中是什么,返回时都是一样的,因此循环继续,并重复第一个问题
使用调试器将帮助您了解其工作原理。谢谢。我试着使用pythontutor.com,但我仍然能够弄清楚发生了什么。也许如果你解释一下你的理解不足的地方,有人可以提供帮助。我的问题是:如何让程序在输入第三个错误猜测后停止,而不是重复第一个问题。你已经问过了;你没有做的是努力解决问题,甚至没有理解为什么会有问题。我正在努力理解它,但我被卡住了,这就是我来这里寻求帮助的原因。谢谢你的鼓励和帮助,专家先生。