Can a def function break a while loop? - python

def CardsAssignment():
Cards+=1
print (Cards)
return break
while True:
CardsAssignment()
Yes, I know that I cannot return break. But how can I break a while loop by the def function? Or my concept is wrong?

No it cannot. Do something like:
def CardsAssignment():
Cards+=1
print (Cards)
if want_to_break_while_loop:
return False
else:
return True
while True:
if not CardsAssignment():
break

A very Pythonic way to do it would be to use exceptions with something like the following:
class StopAssignments(Exception): pass # Custom Exception subclass.
def CardsAssignment():
global Cards # Declare since it's not a local variable and is assigned.
Cards += 1
print(Cards)
if time_to_quit:
raise StopAssignments
Cards = 0
time_to_quit = False
while True:
try:
CardsAssignment()
except StopAssignments:
break
Another, less common approach would be to use a generator function which will return True indicating that it's time to quit calling next() on it:
def CardsAssignment():
global Cards # Declare since it's not a local variable and is assigned.
while True:
Cards += 1
print(Cards)
yield not time_to_quit
Cards = 0
time_to_quit = False
cr = CardsAssignment() # Get generator iterator object.
next(cr) # Prime it.
while next(cr):
if Cards == 4:
time_to_quit = True # Allow one more iteration.

You could have CardsAssignment return True (to continue) or False (to stop) and then have
if not CardsAssignment():
break
or indeed just loop
while CardsAssignment():

If you use a for loop instead of a while, you can cause it to break early by raiseing StopIteration - this is the usual signal for a for loop to finish, and as an exception, it can be nested inside functions as deep as you need and will propagate outward until it is caught. This means you need something to iterate over - and so, you probably want to change your function into a generator:
def cards_assignment():
cards += 1
yield cards
for card in cards_assignment():
print(card)
, in which case instead of doing raise StopIteration, you would just return from the generator and the loop will finish. However, note that this (as well as options having the function return a flag that you test in the loop condition) is subtly different to using break - if you use an else clause on your loop, returning from a generator will trigger it, whereas break in the loop body won't.

def CardsAssignment():
Cards+=1
print (Cards)
if (break_it):
return False
else:
return True
while CardsAssignment():
pass

I would be tempted to just re-factor similar to:
def somefunc():
from random import randint
while True:
r = randint(1, 100)
if r != 42:
yield r
And then you can do things such as:
for cnt, something in enumerate(somefunc(), start=1):
print 'Loop {} is {}'.format(cnt, something)
This allows a possible meaningful value to be returned from somefunc() instead of using it as a "do I break" flag.
This will also allow the following construct:
sf = somefunc()
for something in sf:
if some_condition(something):
break
# other bits of program
for something in sf: # resume...
pass

in 3.5, 3.6 you can
return "break"

Related

Is there an “elif” equivalent in for/else statements

In if/elif/else statements, elifs are checked if the if is false, and if both the if and elifs are false then the else is run.
For loops have something similar with the else keyword - if the loop doesn’t encounter a break, then the code inside else is run.
So, for loops have an else equivalent to if/elif/else statements. My question is however, is there an equivalent to elif.
I’m thinking that it would be something along the lines of:
Run for loop. If no break is encountered, go onto the next
specified loop
Run the specified for loop, if no break is encountered, go onto the
next.
Repeat until no more loops are left, and if no breaks have been
encountered up to this point, run the else.
I’m aware you can emulate this effect. Just as you can do this with if/else statements to emulate the elif:
if condition:
do_something()
else:
if another_condition:
do_something_else()
else:
default()
you can do this with for loops:
for x in y:
if condition:
break
else:
for n in m:
if another_condition:
break
else:
default()
While the above code is valid, like with if/else statements, it’s faily ugly and I was hoping to find a better alternative.
No. If you look at the grammar of the for statement:
for_stmt:
| 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block]
. . .
It ends in an optional else_block, but nothing else. Your emulation is as good as you'll get I believe.
What if you make the variable that determins whether you should continue global? Since if any of the conditions are met, you don't want to continue with any of the following loops this should work:
def do_something():
for i in range(3):
condition = i < 3
if not condition:
return False
return True
def do_something_else():
for i in range(3):
condition = i < 2
if not condition:
return False
return True
condition_met = True
if condition_met:
print('we start here')
condition_met = do_something()
if condition_met:
print('and should get here')
condition_met = do_something_else()
if condition_met:
print('but not here')
It may not be amazing, but at least it's not nested, and sort of looks like the syntax you're looking for.
You could also use a while loop:
operations = [do_something, do_something_else]
i = 0
while condition_met and i < len(operations):
condition_met = operations[i]()
While the for-loop syntax doesn't allow this (as shown by #Carcigenicate), you can keep nesting levels down by using a try-else:
try:
for x in y:
if z:
raise SomeException
for m in n:
if o:
raise SomeException
except SomeException:
pass
else:
default()
A cleaner way, however, might wrap the loops in functions:
# these can be more complex non-anonymous functions
f1 = lambda: any(1 for x in y if z)
f2 = lambda: any(1 for m in n if o)
f3 = ...
if not any(f() for f in (f1, f2, f3)):
default()
I'm not sure, but maybe while will be ok for you
i = 0
while i < 10:
if condition:
break
i += 1

Redo for loop iteration in Python

Does Python have anything in the fashion of a "redo" statement that exists in some languages?
(The "redo" statement is a statement that (just like "break" or "continue") affects looping behaviour - it jumps at the beginning of innermost loop and starts executing it again.)
No, Python doesn't have direct support for redo. One option would something faintly terrible involving nested loops like:
for x in mylist:
while True:
...
if shouldredo:
continue # continue becomes equivalent to redo
...
if shouldcontinue:
break # break now equivalent to continue on outer "real" loop
...
break # Terminate inner loop any time we don't redo
but this mean that breaking the outer loop is impossible within the "redo-able" block without resorting to exceptions, flag variables, or packaging the whole thing up as a function.
Alternatively, you use a straight while loop that replicates what for loops do for you, explicitly creating and advancing the iterator. It has its own issues (continue is effectively redo by default, you have to explicitly advance the iterator for a "real" continue), but they're not terrible (as long as you comment uses of continue to make it clear you intend redo vs. continue, to avoid confusing maintainers). To allow redo and the other loop operations, you'd do something like:
# Create guaranteed unique sentinel (can't use None since iterator might produce None)
sentinel = object()
iterobj = iter(mylist) # Explicitly get iterator from iterable (for does this implicitly)
x = next(iterobj, sentinel) # Get next object or sentinel
while x is not sentinel: # Keep going until we exhaust iterator
...
if shouldredo:
continue
...
if shouldcontinue:
x = next(iterobj, sentinel) # Explicitly advance loop for continue case
continue
...
if shouldbreak:
break
...
# Advance loop
x = next(iterobj, sentinel)
The above could also be done with a try/except StopIteration: instead of two-arg next with a sentinel, but wrapping the whole loop with it risks other sources of StopIteration being caught, and doing it at a limited scope properly for both inner and outer next calls would be extremely ugly (much worse than the sentinel based approach).
No, it doesn't. I would suggest using a while loop and resetting your check variable to the initial value.
count = 0
reset = 0
while count < 9:
print 'The count is:', count
if not someResetCondition:
count = count + 1
This is my solution using iterators:
class redo_iter(object):
def __init__(self, iterable):
self.__iterator = iter(iterable)
self.__started = False
self.__redo = False
self.__last = None
self.__redone = 0
def __iter__(self):
return self
def redo(self):
self.__redo = True
#property
def redone(self):
return self.__redone
def __next__(self):
if not (self.__started and self.__redo):
self.__started = True
self.__redone = 0
self.__last = next(self.__iterator)
else:
self.__redone += 1
self.__redo = False
return self.__last
# Display numbers 0-9.
# Display 0,3,6,9 doubled.
# After a series of equal numbers print --
iterator = redo_iter(range(10))
for i in iterator:
print(i)
if not iterator.redone and i % 3 == 0:
iterator.redo()
continue
print('---')
Needs explicit continue
redone is an extra feature
For Python2 use def next(self) instead of def __next__(self)
requires iterator to be defined before the loop
I just meet the same question when I study perl,and I find this page.
follow the book of perl:
my #words = qw(fred barney pebbles dino wilma betty);
my $error = 0;
my #words = qw(fred barney pebbles dino wilma betty);
my $error = 0;
foreach (#words){
print "Type the word '$_':";
chomp(my $try = <STDIN>);
if ($try ne $_){
print "Sorry - That's not right.\n\n";
$error++;
redo;
}
}
and how to achieve it on Python ??
follow the code:
tape_list=['a','b','c','d','e']
def check_tape(origin_tape):
errors=0
while True:
tape=raw_input("input %s:"%origin_tape)
if tape == origin_tape:
return errors
else:
print "your tape %s,you should tape %s"%(tape,origin_tape)
errors += 1
pass
all_error=0
for char in tape_list:
all_error += check_tape(char)
print "you input wrong time is:%s"%all_error
Python has not the "redo" syntax,but we can make a 'while' loop in some function until get what we want when we iter the list.
Not very sophiscated but easy to read, using a while and an increment at the end of the loop. So any continue in between will have the effect of a redo. Sample to redo every multiple of 3:
redo = True # To ends redo condition in this sample only
i = 0
while i<10:
print(i, end='')
if redo and i % 3 == 0:
redo = False # To not loop indifinively in this sample
continue # Redo
redo = True
i += 1
Result: 00123345667899
There is no redo in python.
A very understandable solution is as follow:
for x in mylist:
redo = True
while redo:
redo = False
If should_redo:
redo = True
It's clear enough to do not add comments
Continue will work as if it was in the for loop
But break is not useable, this solution make break useable but the code is less clear.
Here is a solution for python 3.8+ since now we have the := operator:
for key in mandatory_attributes: # example with a dictionary
while not (value := input(f"{key} (mandatory): ")):
print("You must enter a value")
mandatory_attributes[key] = value

Start a new iteration of a loop from a different function in the loop [python]

How do you go about starting the next i without using continue or break?
def function_in_main():
if #something happens:
#start new next iteration for the loop in the main function
def main():
n = 1
for i in range(len(alist)):
print ('step ', n)
function_in_main()
n += 1
main()
Output should look somewhat like:
step 1
#if or until something happens
step 2
etc
Just make function_in_main return when your if statement is true. when it returns, the loop will move on to the next iteration and then re-call function_in_main.
Maybe try raising an exception:
def function_in_main():
if #something happens:
raise Exception
def main():
n = 1
for i in range(len(alist)):
print ('step ', n)
try:
x()
except Exception:
continue
n += 1
main()
You can specify or make whatever type of exception you want.
Here is an example:
def function_in_main(x):
return x=='Whopee' # will return True if equal else False
def main():
alist=['1','ready','Whopee']
for i,item in enumerate(alist, 1):
print ('step {}, item: "{}" and {}'.format(i,item,function_in_main(item)))
main()
Prints:
step 1, item: "1" and False
step 2, item: "ready" and False
step 3, item: "Whopee" and True
Note the use of enumerate rather than manually keeping a counter.
Sure you're not thinking of a while loop or something? It's a little tough to see what you're getting at…
def your_function():
#…do some work, do something to something else
if something_is_true:
return True
else:
return False
def main():
condition = True
for i in range(len(your_list)):
while condition:
print "Step: %d" % i
your_function()
Only when your_function returns False will the loop in main continue, otherwise it will stay within the while loop.

break and continue in function

def funcA(i):
if i%3==0:
print "Oh! No!",
print i
break
for i in range(100):
funcA(i)
print "Pass",
print i
I know script above won't work. So, how can I write if I need put a function with break or continue into a loop?
A function cannot cause a break or continue in the code from which it is called. The break/continue has to appear literally inside the loop. Your options are:
return a value from funcA and use it to decide whether to break
raise an exception in funcA and catch it in the calling code (or somewhere higher up the call chain)
write a generator that encapsulates the break logic and iterate over that instead over the range
By #3 I mean something like this:
def gen(base):
for item in base:
if item%3 == 0:
break
yield i
for i in gen(range(1, 100)):
print "Pass," i
This allows you to put the break with the condition by grouping them into a generator based on the "base" iterator (in this case a range). You then iterate over this generator instead of over the range itself and you get the breaking behavior.
Elaborating BrenBarns answer: break fortunately will not propagate. break is to break the current loop, period. If you want to propagate an event, then you should raise an exception. Although, raising the exception to break the loop is a really ugly way to break loops and a nice way to break your code.
KISS! The simplest would be to check the condition directly in the loop
def my_condition(x):
return x == 4
for i in xrange(100):
if my_condition(i): break
print i
If, for some reason, you want to propagate an exception, then you use it like this
# exception example
for i in xrange(100):
if i == 4: raise Exception("Die!")
print i
As mentioned, it is a really ugly design. Imagine you forget to catch this exception, or you change its type from Exception to MyBreakException and forget to change it somewhere in try/except higher part of the code...
The generator example has its merits, it makes your code more functional style (which I presonally adore)
# generator example
def conditional_generator(n, condition):
for i in xrange(n):
if condition(i):
break
else:
yield i
for i in conditional_generator( 100, my_condition ):
print i
...which is similar to takewhile, mentioned by eumiro
def funcA(i):
if i%3==0:
print "Oh! No!",
print i
return True
else:
return False
for i in range(100):
if funcA(i):
break
print "Pass",
print i
Break won't propagate between functions, you need to put it directly within the loop somewhere.

Using for...else in Python generators

I'm a big fan of Python's for...else syntax - it's surprising how often it's applicable, and how effectively it can simplify code.
However, I've not figured out a nice way to use it in a generator, for example:
def iterate(i):
for value in i:
yield value
else:
print 'i is empty'
In the above example, I'd like the print statement to be executed only if i is empty. However, as else only respects break and return, it is always executed, regardless of the length of i.
If it's impossible to use for...else in this way, what's the best approach to this so that the print statement is only executed when nothing is yielded?
You're breaking the definition of a generator, which should throw a StopIteration exception when iteration is complete (which is automatically handled by a return statement in a generator function)
So:
def iterate(i):
for value in i:
yield value
return
Best to let the calling code handle the case of an empty iterator:
count = 0
for value in iterate(range([])):
print value
count += 1
else:
if count == 0:
print "list was empty"
Might be a cleaner way of doing the above, but that ought to work fine, and doesn't fall into any of the common 'treating an iterator like a list' traps below.
There are a couple ways of doing this. You could always use the Iterator directly:
def iterate(i):
try:
i_iter = iter(i)
next = i_iter.next()
except StopIteration:
print 'i is empty'
return
while True:
yield next
next = i_iter.next()
But if you know more about what to expect from the argument i, you can be more concise:
def iterate(i):
if i: # or if len(i) == 0
for next in i:
yield next
else:
print 'i is empty'
raise StopIteration()
Summing up some of the earlier answers, it could be solved like this:
def iterate(i):
empty = True
for value in i:
yield value
empty = False
if empty:
print "empty"
so there really is no "else" clause involved.
As you note, for..else only detects a break. So it's only applicable when you look for something and then stop.
It's not applicable to your purpose not because it's a generator, but because you want to process all elements, without stopping (because you want to yield them all, but that's not the point).
So generator or not, you really need a boolean, as in Ber's solution.
If it's impossible to use for...else in this way, what's the best approach to this so that the print statement is only executed when nothing is yielded?
Maximum i can think of:
>>> empty = True
>>> for i in [1,2]:
... empty = False
... if empty:
... print 'empty'
...
>>>
>>>
>>> empty = True
>>> for i in []:
... empty = False
... if empty:
... print 'empty'
...
empty
>>>
What about simple if-else?
def iterate(i):
if len(i) == 0: print 'i is empty'
else:
for value in i:
yield value

Categories