samedi 4 mai 2019

Is there a Python 3 command that when run could send me back to the top of a while loop and rerun it? [duplicate]

This question already has an answer here:

I was wondering if there was a command in Python 3 that could be nested within a while loop and when run would return to the top of the loop (including checking the condition to decide to reenter the loop or not)? I have a while loop that contains multiple if/elif statements, I'm looking for a way that if a condition is met (lets say an if statement), the command would follow and bring me back to the top of the while loop instead of moving on to test the next condition (elif statement).

I've tried utilizing break statements but my problem is I want to stay in the while loop and reiterate through it (as opposed to breaking out of it entirely). I've also tried continue statements (but I may be using them wrong).

Here is an example of what I'm trying to accomplish

a = 0
while(a <= 5):
    a += 1
    if a == (1 or 2 or 3):
        print('1')
        # some command here that sends me back to the top of the while 
        # loop (line 2: to check if a<= 5)
    elif a <= 5:
        print('2')

######

#The result I'm looking for would print:
1
1
1
2
2

#The result I get is:
1
2
2
2
2

The result I actually get shows that the program stays within the while loop and moves on to the next elif condition. I'm hoping to find a way to send the program back to the top of the while loop and rerun the conditions from there.

Many Thanks!

Aucun commentaire:

Enregistrer un commentaire