This exercise will show case two things:
1. Control structures
2. How to structure your program correctly
Similar to other languages Python support various control structures such as:
1. If...else
2. For
3. While
4. Break, continue
5. But, no swtich
DRY stands for Don't repeat yourselves
One should not repeat the same functionality or copy the same code again. So, we use functions just like the one we did in the previous exercise.
However, the way we structured the program does not work well in the context of import.
In [1]:
def print_n_stars(n):
"""
Prints n no. of stars
Arguments:
n: number of stars to print
"""
for i in range(n):
print "*", # having a ',' tells print not to insert a new line after print
print '' # inserts a new line after the loop
return
In [2]:
if __name__ == '__main__':
m = 5
i = 0
while True:
if i < m:
i = i +1
print_n_stars(i)
elif i==m:
i = i +1
print('Elif reached')
else:
print('Stopping the loop')
break
However, this function call will not be called when imported in other scripts.