Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Breaking While Loop
#1
Hello,

I have potentially eight sequential ifs I need to test for, so instead of nesting eight if statements I thought to use a while loop.
My goal is for the while loop to end once one of my functions returns a value greater than 0. Is there a better way to achieve this
than my method below?

x = 0

while x == 0:

x = function returning 0, 1, or 2
if x > 0:
break

x = function with different arguments returning 0, 1, or 2
if x > 0:
break

x = function with different arguments returning 0, 1, or 2
if x > 0:
break

x = function with different arguments returning 0, 1, or 2
if x > 0:
break

x = function with different arguments returning 0, 1, or 2
if x > 0:
break

x = function with different arguments returning 0, 1, or 2
if x > 0:
break

x = function with different arguments returning 0, 1, or 2
if x > 0:
break

x = function with different arguments returning 0, 1, or 2
if x > 0:
break

x = 3
Reply
#2
Youve abstracted this too much for me to help. Can you clarify the conditions you are testing?
Reply
#3
(Oct-27-2019, 12:50 AM)jefsummers Wrote: Youve abstracted this too much for me to help. Can you clarify the conditions you are testing?

I'm trying to write what's below more concisely. I'm wondering if this can be achieved using a while loop.

if function(first_arg) > 0:
    x = function(first_arg)

elif function(second_arg) > 0:
    x = function(second_arg)

elif function(third_arg) > 0:
    x = function(third_arg)

# .
# .
# .

elif function(eighth_arg) > 0:
    x = function(eighth_arg)
Reply
#4
If I understand correctly it can be done with for-loop:

lst = [0, -2, -5, 6, 8]

def my_func(num):
    return num

for item in lst:
    x = my_func(item)
    if x > 0:
        break
Of course one can have onliner fun as well:

next((item for item in lst if my_func(item) > 0))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thank you!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ChromeDriver breaking Python script genericusername12414 1 291 Mar-14-2024, 09:39 AM
Last Post: snippsat
  Openpyxl module breaking my code EnderSM 5 1,049 May-26-2023, 07:26 PM
Last Post: snippsat
  breaking out of nested loops Skaperen 3 1,206 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  How to use += after breaking for loop? Frankduc 2 1,482 Dec-31-2021, 01:23 PM
Last Post: Frankduc
  How to fix while loop breaking off raphy11574 3 2,171 Oct-12-2021, 12:56 PM
Last Post: deanhystad
  breaking a large program into functions, not acting as expected Zane217 9 3,006 Sep-18-2021, 12:37 AM
Last Post: Zane217
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,351 Sep-04-2021, 04:51 PM
Last Post: imillz
Exclamation Help in breaking bytes into bits through masking and shifting kamui123 9 4,532 Jan-11-2021, 07:42 AM
Last Post: kamui123
  breaking outof nexted loops Skaperen 4 3,105 Feb-07-2020, 02:04 AM
Last Post: Skaperen
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,885 Sep-18-2019, 09:43 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020