Martin Bergeron
Martin Bergeron

Reputation: 129

checking if a loop returned a value

I am trying to figure out how to test my list if it ever give any result at all then give the ok to run an other list. Do i have to make a new 'for' statement that run my list again? sry for bad english

I found these statement but im trying to figure out how i can apply them to my code.

But how do I check if my loop never ran at all?

The easiest way to check if a for loop never executed is to use None as a sentinel value:

x = None
for x in data:
    ... # process x
if x is None:
    raise ValueError("Empty data iterable: {!r:100}".format(data))
If None is a legitimate data value, then a custom sentinel object can be used instead:

x = _empty = object()
for x in data:
    ... # process x
if x is _empty:
    raise ValueError("Empty data iterable: {!r:100}".format(data))

My usecase:

message = ["potato:23", "orange:", "apple:22"]

for i in message:
    parts = i.split(":")
    gauche = parts[0].strip()
    droite = parts[1]

    try:
        droite = int(droite)
        if not gauche.isalpha():
            print("La ligne '", i, "' n'est pas correctement formaté.")
            break
        except ValueError:
            print("La ligne '", i, "' n'est pas correctement formaté.")
            break

# following code runs if no error was raised in the for loop
message2 = sorted(ligne(texte))
for j in message2:
    print(j)
sys.exit()

Upvotes: 1

Views: 81

Answers (1)

stovfl
stovfl

Reputation: 15513

Question: I would like to execute this like of code if that for didnt raise any error

As you want to continue your programm, you couldn't use raise.
For instance, I append all gauche if int(droite) didn't raise ValueError and gauche.isalpha.
Sort this list of fruits and print it.

ligne = []
for i in message:
    parts = i.split(":")
    gauche = parts[0].strip()
    droite = parts[1]
    try:
        droite = int(droite)
        if not gauche.isalpha():
            #raise Exception("La ligne {!r:2} n'est pas correctement formaté.".format(i))
            print("La ligne {!r:2} n'est pas correctement formaté.".format(i))
        else:
            # Append only if isalpha
            ligne.append((gauche))

    except ValueError:
        #raise Exception("La ligne {!r:2} n'est pas correctement formaté.".format(i))
        print("La ligne {!r:2} n'est pas correctement formaté.".format(i))


message2 = sorted(ligne)
for fruit in message2:
    print(fruit)

Output:
La ligne 'orange:' n'est pas correctement formaté.
apple
potato


Tried your code, it's working for me. I get the following output:

La ligne ' orange: ' n'est pas correctement formaté.

Question:I found these statement but im trying to figure out how i can apply them to my code

You can use it for instance:

try:
    droite = int(droite)
    if not gauche.isalpha():
        raise Exception("La ligne {!r:2} n'est pas correctement formaté.".format(i))
except ValueError:
    raise Exception("La ligne {!r:2} n'est pas correctement formaté.".format(i))

Upvotes: 1

Related Questions