Back to: Python Programming
0
A nested loop is a loop that runs inside another loop. The outer loop has a set number of iterations, for each of these iterations the inner loop executes its iterations.
for x in range(3):
for y in range(1, 11):
print(y, end=" ")
print() # <-- Start a new line Outer Loop
Exercise:
Write code to ask a user for the dimensions of a rectangle, then print it out using a user defined character.
Solution:
length = int(input("Enter the length of your rectangle: "))
width = int(input("Enter the width of your rectangle: "))
symbol = input("Enter the symbol to draw your rectangle: ")
for i in range(length):
for j in range(width):
print(symbol, end="")
print()
Output:
Enter the length of your rectangle: 2
Enter the width of your rectangle: 40
Enter the symbol to draw your rectangle: #
########################################
########################################
Recursion Simply Explained with Code Examples – Python for Beginners – (9m:06s)