How do I stop For when the range is 5?

Asked

Viewed 258 times

-4

How do I stop the For cycle when it reaches a certain condition ? For example, it would be when c equals 5.

for c in range(1,10):
    print('a,b,c')

if c % 5:
    break

Then a function will be used, but I would like to understand the flow control in python.

  • 3

    it’s not better just to do range(1, 5)?

  • 2

    range(0,10) to break into 5 unconditionally makes no sense

  • 2

    This looks like a XY problem. It would be better to try to explain what you are really trying to do and prefaced with real code, so that it is clear to everyone.

  • 3

    Now the question has changed, which has made the situation worse. You have an answer and accept, so changing the question makes it invalid. , you cannot do this.

1 answer

-2


Hello, Voce can use it like this:

for c in range(1,10):
    print('a,b,c')
    if c % 5:  # Condição 
       break

The if c == 5: must be within the cycle for.

If you want to skip only one cycle you can use the continue in place of break.

Extra :

As I saw in the comments above, you want to use a function when arriving at a certain value.

  • Attention : it would make no sense to interrupt the cycle for for a crease, 'cause you could use it this way:

    for c in range(1,x):
            print('a,b,c')
    
    • Now the x will be the stopping condition. "Avoiding the use of the if" .

The question code can look like this:

def funcaoStack(): # Função de exemplo, que será chamada dentro do ciclo do FOR.
    print('foo!!!')

def funcaoOverflow(): # Função de exemplo, que será chamada após o ciclo FOR.
    print('saa!!!')

for i in range(10):

    if i % 5: # Essa fução irá ser chamada quando i for igual a 5.
        funcaoStack()
        continue # Foi adicionado o "continue" para demonstração. 

    if i == 7: # Aqui o ciclo FOR será interrompido, ai continua o'que está no script. 
        break

    print(i) # Por que o "print" está depois do "if" ?
             # Pois para fazer sentido na resposta, primeiro se verifica as condições no ciclo.

funcaoOverflow()
  • 2

    This doesn’t make any sense, even though it works.

  • 2

    @Maniero I think it makes sense because fundamentally the question is "how to stop the loop when a certain condition is reached", and the condition being c == 5 is only the easiest/obvious case. A good answer would explain how to do it, and also why it is not a good idea.

  • @Maniero also do not find it good practice to answer a question and then close it.

  • @I edited my answer so that it makes more sense, with an example of use.

  • 2

    @Pedrovonhertwig And doing this makes no sense, no zenith, zero. This site is designed to teach people to code better, to solve real problems, not to make meaningless code, so the question and this answer is a disservice. I’m sure a lot of people will see this and think it’s the right thing to do, even today that people don’t want to see context. It is unfortunate and a described for the site to have something like this. Teaching wrong is never a good thing.

  • 2

    @Davimello as the question makes no sense I erased it. I thought it was a question where one expected a correct answer and could learn something correct.

  • 3

    Dear @Pedrovonhertwig I think he did not close exactly after answering, he replied believing that the question was a code from which could "be improved", I mean, I agree, a break for a range doesn’t make much sense, so he tried to show a "better" path, the AP commented back but did not present reasons for why he rightly used "RANGE"... like, this is a simulation of data entry? Or is this confusion of the AP? Not clear, so as AP did not clear the reason it closed properly "not this clear" [...]

  • 3

    [...] @Pedrovonhertwig something I see that people still don’t understand, closing is not the same thing as deleting forever, almost everything that is closed is just to avoid rash responses, if closed can be reopened at any time, the close is the opportune moment for AP to edit and clarify your question, I recommend you read: https://pt.meta.stackoverflow.com/a/2676/3635 ... I hope I’ve managed to give you a north and hope the question can be "saved" as soon as the AP clears up the need for this.

  • 1

    I take this opportunity to add that the if unconditional in 5 and in the 7 in both examples they do not make sense. It’s the same as me filling up my car with gasoline to do 300 km but I know that at 100 km I will go off the road and reach my destination.

  • @Isac ; Ready, I changed the answer to make sense for everyone.

  • 1

    Now there is no more to save here, I made a mistake, I should have closed from the beginning, had indications that would be a problem, but the staff complains that closing too fast, here is a show because closing fast, would have saved the whole problem. With closure and care who knows if something good would come out of it.

  • @Maniero ; I’m new to the stack, but the problem is in the question or my answer ? And how could it be done right ?

  • 2

    Especially in the question, but the answer ends up going down a complicated path that leads people to error. In fact it would most likely close this as "not a real question", but that reason no longer exists. see https://answall.com/help/on-topic and https://answall.com/help/how-to-ask. needs to be a practical, real problem, something that reads the right one. Here we try to consider good faith until proven otherwise. Wanting to learn right even though in a clumsy way all right, but it was clear that it wasn’t what you wanted, and you kept an open mind for a different solution

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.