Skip to content

Commit df85f0b

Browse files
committed
Function inside a for loop along with exception handling
1 parent 2dbe6c9 commit df85f0b

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def create_groups(items, n):
2+
try:
3+
size = len(items) // n
4+
except ZeroDivisionError as e:
5+
print("WARNING: Please use a nonzero number. Error returned is {}".format(e))
6+
return []
7+
else:
8+
groups = []
9+
for i in range(0, len(items), size):
10+
groups.append(items[i:i + size])
11+
return groups
12+
finally:
13+
print("{} groups returned.".format(n))
14+
15+
print("Creating 6 groups...")
16+
for group in create_groups(range(64), 6):
17+
print(list(group))
18+
19+
print("\nCreating 0 groups...")
20+
for group in create_groups(range(32), 0):
21+
print(list(group))

0 commit comments

Comments
 (0)