Skip to content Skip to sidebar Skip to footer

How To Return A Specific Point After An Error In 'while' Loop

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('...

Solution 1:

[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")

Solution 2:

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)

Solution 3:

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


Solution 4:

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")

Solution 5:

Yes, there is no way to return to a prior line in code after execution except via a loop. No way at all.

Python and many modern programming languages work in this way and do not support a "goto" line.

Hence, this is the reason the only way to do it is by some form of multiple while loops to execute a statement repeatedly till your desired outcome was received (Either nested loops, or by pulling out the while loop into a function as suggested by salparadise).


Post a Comment for "How To Return A Specific Point After An Error In 'while' Loop"