0

The program I have is rather complicated so I've just decided to take this portion. Basically, this code is supposed to loop through a series of lines and stop once TIME has reached 4800; however, if quepax has reached 15 in a loop, it will evaluate R. If the loop is not yet broken by TIME>=4800, subsequent loops will not get new values of R (since quepax will always be more than 15 from there) while still going through the rest of the lines. Below is an attempt to do such a program. It sort of works, but is it possible to make a simpler code without repeating content?

    while True:
            i+=1
            pax[i]=random.randint(1,5)
            TOTALPAX+=pax[i]
            TIME+=between[i]
            queue+=pax[i]
            if quepax>15:
                R=i-1
                while True:
                    i+=1
                    pax[i]=random.randint(1,5)
                    TOTALPAX+=pax[i]
                    TIME+=between[i]
                    queue+=pax[i]
                    if TIME>=4800:
                        break
            if TIME>=4800:
                break
txsaw1
  • 121
  • 13
  • I would probably not put this in nested loops. – Hellmar Becker Nov 14 '15 at 12:02
  • Yes, so I'm wondering if I can do this without a nested loop. – txsaw1 Nov 14 '15 at 12:03
  • To "skip lines given a condition", you'd do `if not "condition": `. Can you be more clear about your goal? Write down exactly what you want, in a way that does not need any more explanations. From there to code, it should be only a small step left. – Jasper Nov 14 '15 at 12:04
  • 1
    "Not sure if it works yet." You mean you haven't tried this actual program and see what happens? –  Nov 14 '15 at 12:14
  • One classic way to avoid a double break is to put the whole code in a function, and `return` and instead of `break`. –  Nov 14 '15 at 12:14
  • I will probably try that. I heard that `return` can act as a control flow. http://stackoverflow.com/questions/7129285/why-would-you-use-the-return-statement-in-python – txsaw1 Nov 14 '15 at 12:25
  • Well I realized that I want to simply avoid repeating the lines, and the double break is not so important. Sorry about that. – txsaw1 Nov 14 '15 at 12:43

1 Answers1

0

I managed to find this solution which eliminates the repeated parts, but it still looks rather unconventional. Is there an alternative to this?

    run=0
    while True:
            i+=1
            pax[i]=random.randint(1,5)
            TOTALPAX+=pax[i]
            TIME+=between[i]
            queue+=pax[i]
            if quepax>15 and run=0:
                R=i-1
                run=1
            if TIME>=4800:
            break
txsaw1
  • 121
  • 13
  • If you try to do a thing at most once in a loop, it's not unconventional to use a flag for this. You should rename `run` to something more meaningful and use boolean values. For example: `setR = True; ... if quepax > 15 and setR: R=i-1; setR=False` – Jasper Nov 14 '15 at 12:58
  • Thanks. I was just trying to think of something quick on the spot, so apologies for that. But I think that solved my problem. – txsaw1 Nov 14 '15 at 13:44
  • It indeed looks a bit odd for typical Python code with all that indexing but it is hard to suggest anything different with all those ”holes”. `quepax` is never changed in the shown code so it is either always >15 or never. `queue` and `TOTALPAX` are incremented in sync and not used in that loop so you could add up in a temporary variable and add the result to both after the loop. Instead of `while` and incrementing `i` manually a `for` loop would be the natural choice. – BlackJack Nov 15 '15 at 00:24