GLH*_*LHF 13 python loops continue while-loop python-3.x
我正在尝试编写一个包含while循环的程序,在这个循环中,如果出现问题,我会收到错误消息.有点像这样;
while True:
questionx = input("....")
if x =="SomethingWrongabout questionX":
print ("Something went wrong.")
continue
other codes...
questiony = input("....")
if y == "SomethingWrongabout questionY":
print ("Something went wrong.")
continue
other codes...
questionz = input("....")
if z == "SomethingWrongabout questionZ":
print ("Something went wrong.")
continue
other codes..
Run Code Online (Sandbox Code Playgroud)
问题如下:当发生错误后questionX,程序进入开始状态.它从头开始,而不是从y或z.但是,x没有问题,因此,程序应该开始提出问题y或z因为,问题发生在y或z.
如何使程序从特定点开始,如果只有y问题出错,程序必须开始提问y或者只是在z,程序必须从头开始z,而不是从头开始x.
我应该while为此使用多个循环,还是有任何东西只能在一个循环中使用?
[从发电机到功能的编辑]
你可以尝试一个功能:
def check_answer(question, answer):
while True:
current_answer = input(question)
if current_answer == answer:
break
print "Something wrong with question {}".format(question)
return current_answer
answerX = check_answer("Question about X?\n", "TrueX")
answerY = check_answer("Question about Y?\n", "TrueY")
answerZ = check_answer("Question about Z?\n", "TrueZ")
Run Code Online (Sandbox Code Playgroud)
不确定是否要保留这些值,但是如果你需要调整它,这应该给你提示.
结果:
Question about X?
"blah"
Something wrong with question Question about X?
Question about X?
"blah"
Something wrong with question Question about X?
Question about X?
"TrueX"
Question about Y?
"TrueY"
Question about Z?
"blah"
Something wrong with question Question about Z?
Question about Z?
"blah"
Something wrong with question Question about Z?
Question about Z?
"TrueZ"
Run Code Online (Sandbox Code Playgroud)
根据评论编辑:
def check_answer(question, answers):
while True:
current_answer = input(question)
if current_answer in answers:
break
print "Something wrong with question {}".format(question)
return current_answer
answerX = check_answer("Question about X?\n", ("TrueX", "TrueY")
Run Code Online (Sandbox Code Playgroud)
我认为这里有两个非常简单,优雅的解决方案.
这个想法是有一个问题列表.只要问题仍然存在,两种实现都会继续询问.itertools.dropwhile()只要问题的答案是正确的,另一个做不同的事情,就会使用该方法从列表中删除元素 - 见下文.
在这个示例实现中,神奇的答案'foo'是对任何问题的错误答案.您可以在Python中运行它来检查它是否会重新询问您回答'foo'的问题的(剩余)问题.
通过修改ask_question()功能可以直接适应您的情况.
import itertools
input = lambda x: raw_input("what is your "+x+"? ")
# returns true or false; wether or not the question was answered
# correctly
def ask_question(question):
answer = input(question)
# could be any test involving answer
return answer != "foo"
# assume we have a list of questions to ask
questions = [ "age", "height", "dog's name" ]
# keep on looping until there are questions
while questions:
questions = list(itertools.dropwhile(ask_question, questions))
Run Code Online (Sandbox Code Playgroud)
编辑
所以,在幕后,还有两个while循环(这takewhile()是赠品:-)).有了一些开箱即用的想法,它甚至可以在没有一个while循环的情况下完成:
递归就是这个词!
def ask_more_questions(question_list):
# no more questions? then we're done
if not question_list:
return
# ask the first question in the list ...
if ask_question(question_list[0]):
# ok, this one was answered fine, continue with the remainder
ask_more_questions(question_list[1:])
else:
# Incorrect answer, try again with the same list of questions
ask_more_questions(question_list)
Run Code Online (Sandbox Code Playgroud)
可以压缩到,如果你喜欢:
def ask(q_list):
if qlist:
ask(q_list[1:]) if ask_question(q_list[0]) else ask(q_list)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1555 次 |
| 最近记录: |