mardav
mardav

Reputation: 67

While/Else Recheck the condition and loop

I have the following code:

def check(onen,twon,threen,fourn,fiven):
while ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven)):
    return onen
else:
    onen = random.randint(1,45)

I'd like to ask how to make it like this:

def check(onen,twon,threen,fourn,fiven):
while ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven)):
    return onen
else:
    onen = random.randint(1,45)
        (check the condition on while again)

I want to make this loop: if the condition is false, check and check again until it's true.

Upvotes: 2

Views: 1497

Answers (5)

tobias_k
tobias_k

Reputation: 82949

It seems like you have it backwards. Try this:

while not condition:
    change condition
return that

For your specific example:

def check(onen, twon, threen, fourn, fiven):
    while not ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven)):
        onen = random.randint(1,45)
    return onen

Or shorter:

def check(onen, twon, threen, fourn, fiven):
    while onen in (twon, threen, fourn, fiven):
        onen = random.randint(1,45)
    return onen

Or much shorter, without the loop (only feasible for small range, though):

def check(onen, twon, threen, fourn, fiven):
    return random.choice([x for x in range(1, 46) 
                          if x not in (twon, threen, fourn, fiven)])

Note, however, that neither of those will change the value of onen outside of the function (unless, of course, you do onen = check(...)).

Upvotes: 1

Zeeshan Hyder
Zeeshan Hyder

Reputation: 518

You can try this also:

something(condition):
    # evaluate and return condition value
while(condition is not satisfactory):
    condition = something(condition)
else:
 # code post satisfaction of condition

Upvotes: 0

quamrana
quamrana

Reputation: 39414

From the update to your question it seems you just need to invert the sense of the while to get what you want:

def condition(onen, twon, threen, fourn, fiven):
    return ((onen != twon) and (onen != threen) and (onen != fourn) and (onen != fiven))

def check(onen, twon, threen, fourn, fiven):
    while not condition(onen, twon, threen, fourn, fiven):
        onen = random.randint(1,45)
    return onen

Upvotes: 0

ntnlbar
ntnlbar

Reputation: 147

def something(): 
    while(condition): 
        return that 
    else: 
        return this
        something() # just callback the function

You can also remove the else statment and just callback the current function

Upvotes: -1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477676

What you are basically looking for is a do-while loop. Python has no do-while loop, but you can easily emulate one:

def something():
    while True:
        # ...
        # perform some task
        if [condition]:
            return [result]

So here you have to fill in [condition] that checks if the result is satisfying, and [result] is what you want to return. As long as the condition is not met, Python will go for another loop.

Example:

Say you want to query the user for input, you can do this with:

def something():
    while True:
        try:
            x = int(input('Enter a number'))
        except ValueError:
            x = None

        if x is not None:
            return x

So here we will keep querying for a number until it is a valid one.

Of course we sometimes can fold the task and condition check together. Here we can transform the above program into:

def something():
    while True:
        try:
            return int(input('Enter a number'))
        except ValueError:
            pass

Upvotes: 1

Related Questions