Objective
Develop a Python program that prompts the user for a symbol, a width, and a height, and displays a hollow rectangle of that width and height using that symbol for the outer border, as in this example:
Enter a symbol: 4
Enter the desired width: 3
Enter the desired height: 5
444
4 4
4 4
4 4
444
Example Python Exercise
Show Python Code
# Prompt the user to enter a symbol
symbol = input("Enter a symbol: ")
# Prompt the user to enter the desired width
width = int(input("Enter the desired width: "))
# Prompt the user to enter the desired height
height = int(input("Enter the desired height: "))
# Use a while loop to display the hollow rectangle
i = 0
while i < height:
if i == 0 or i == height - 1:
print(symbol * width)
else:
print(symbol + " " * (width - 2) + symbol)
i += 1
Output
Enter a symbol: 4
Enter the desired width: 3
Enter the desired height: 5
444
4 4
4 4
4 4
444
Share this Python Exercise