I'm trying to write a program that include a while
loop, in this loop I have an error message if something goes wrong. It's kinda like this;
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..
The problem is as follows: when an error occurs after questionX
, the program goes to beginning. It starts from the beginning, not from y
or z
. But at x
there is no problem, so that, the program should start asking questions from y
or z
because, the problem occurred at y
or z
.
How can I make the program start from a specific point, like if there is an error only at y
question, program must start asking questions from y
or if only at z
,program must start from z
, not beginning-not x
.
Should I use more than one while
loop for this or is there anything that makes this work only in one loop?
[EDIT from generator to function]
You can try a function:
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")
Not sure if you want to keep the values, but if you need to tweak it, this should give you hints.
Results:
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"
Edit per comment:
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")
I think here are two, very simple, elegant solutions.
The idea is that there is a list of questions to ask. Both implementations keep on asking as long as questions remain. One will use the itertools.dropwhile()
method to drop elements from the list as long as the answer to the question is correct, the other does something different - see below.
In this example implementation, the magic answer 'foo' is the incorrect answer to any question. You can run this in Python to check that it will restart asking the (remaining) questions at the question where you answered 'foo'.
It should be straightforward to adapt to your situation by modifying the ask_question()
function.
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))
EDIT
So, behind the scenes, there are still two while loops (the takewhile()
is a giveaway :-)). With a bit of thinking-out-of-the-box, it can be done without even a single while loop:
Recursion's the word!
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)
which can be compressed to, if you like:
def ask(q_list):
if qlist:
ask(q_list[1:]) if ask_question(q_list[0]) else ask(q_list)
You are misunderstanding the way to use continue, continue moves to the next iteration of the loop. To fix this just remove the continues
EDIT BASED ON COMMENTS::
I only use the while True
values because I don't know anything else about your system
while True:
while True:
questionx = input("....")
if x =="SomethingWrongabout questionX":
print ("Something went wrong.")
continue
else:
break;
Making use of break
will help you achieve what you want
The problem is going to be solved by multiple while loops. Whether those loops are all on one place, or are factored out into functions/generators/etc., is your choice.
If it were me, I would factor out the question-asking code into a function that takes the question itself, plus the verification code to validate the answer -- the function keeps asking the question until the validation passes:
def ask_question(question, validate):
while "not valid":
answer = input(question)
if validate(answer):
return answer
else:
print(" invalid response, try again")
while True:
x = ask_question("....", lambda a: a=="SomethingWrongabout questionX")
...other codes...
y = ask_questiony("....", lambda a: a== "SomethingWrongabout questionY")
...other codes...
z = ask_questionz("....", lambda a: a=="SomethingWrongabout questionZ")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With