1

This code aims to check whether inputs are in a pre-determined list and, if not, force the user to start over again.

I understand I can use a FOR or WHILE loop and have the IF and ELSE lines nested in the loops. However, I've realised this gets rather messy and tedious on a large scale.

Is there a way to send read previous lines of code after a loop has been exited? The line that concerns us is the one with the "#problem area"; here is my code:

# Variables:

healthy = ["potato", "tomato"]

counter_ = 0

limit_ = 3

while counter_ < limit_:
    
    shopping_list = []
    wanted_item = input("What to get? \n").lower()
    if wanted_item not in healthy:
        counter_ += 1
        print("Try again")
        continue
    if wanted_item in healthy:
        shopping_list.append(wanted_item)
        break
    else:
        break

if counter_ >= limit_:

    print("Start over")
    #problem area

else:

    print("That's a good list")
    print(shopping_list)
Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • Wrapping the whole thing inside another loop is the typical solution. – John Gordon Jul 17 '22 at 15:58
  • What's your intend? You exit the loop when `counter_ >= limit_`, but then you want to jump back into the loop? What's the point? What's the point of the counter if you `break` as soon as a single correct item gets enetered? I would love to help but I really don't understand the objective behind the code. – Finomnis Jul 17 '22 at 16:10
  • This code is supposed to be a simpler, shorter version of my actual coding project, but I've realised this context is poor. The purpose of breaking out of the code is to take an input and crosscheck with a known list; if it isn't there, we try again; if it is, we add it to another list. The purpose of the loop break was to introduce a "do you want to add another item" function, effectively going through the whole "is it on the list, if it is, adds to list". – The Fearsome Asian Jul 17 '22 at 17:04

3 Answers3

0

You can try this. This can cause an infinite loop if the input is never a "potato" or "tomato".

healthy = ["potato", "tomato"]

counter_ = 0

limit_ = 3

while counter_ < limit_:
    
    shopping_list = []
    wanted_item = input("What to get? \n").lower()

    if wanted_item in healthy:
        shopping_list.append(wanted_item)
        break
    else: # if wanted_item not in healthy
        counter_ += 1
        print("Try again")
        continue

    if counter_ >= limit_:

       print("Start over")
       counter_ = 0
    
VRComp
  • 131
  • 1
  • 1
  • 12
0

so if your only issue is that you want to call code again and again until someone has inputted a desired value you can make use of a recursive function

we define a function (here just called function). Everything except the declaration of the healthy array into it. So when the function is now called (with the expression "function()") it will just run the code once like it did in your example. But defining that chunk of code as a function gives us the opportunity to call it again and again and again, so every time the function is called the code inside the function runs from top to bottom.

Now coming to the "problem area" - calling the function() itself right here will call the function each time we reach the "Try again" part of our if statement. -> so we start from the top again.

We will only leave the function if we reach the point where the function does not call itself, which is at "This is a good list"

all we need to do now (after doing the function definition part, is to call the function (this is done by writing function())

I use functions (to be more precise a recursive function) - so if you need additional resources you can use those keywords ("function" - "recursive function") to find them I would highly advise you to get familiar with functions ... functions are an essential part of most programming languages

healthy = ["potato", "tomato"]

def function():

    limit_ = 3

    counter_ = 0

    while counter_ < limit_:
    
        shopping_list = []
        wanted_item = input("What to get? \n").lower()
        if wanted_item not in healthy:
            counter_ += 1
            print("Try again")
            continue
        if wanted_item in healthy:
            shopping_list.append(wanted_item)
            break
        else:
            break

    if counter_ >= limit_:

        print("Start over")
        function()

    else:

        print("That's a good list")
        print(shopping_list)


function()
Lord-JulianXLII
  • 1,031
  • 1
  • 6
  • 16
0

All the answers were helpful and have been implemented in some way. Finally solved my problem by using boolean logic instead of some goto or novel while loop.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '22 at 04:10