In this lab you add nested loops to a Python program provide
In this lab, you add nested loops to a Python program provided. The program should print the outline of the letter E. The letter E is printed using asterisks, three across and five down. This program uses the print(\"*\") to print an asterisk and print(\" \") to print a space. Write the nested loops to control the number of rows and the number of columns that make up the letter E. In the loop body, use a nested if statement to decide when to print an asterisk and when to print a space. The output statements have been written, but you must decide when and where to use them. Execute the program. Observe your output. Modify the program to change the number of rows from five to seven and the number of columns from three to five. What does the letter E look like now? LeteryE.py - This program prints the letter E with 3 asterisks # across and 5 asterisks down. # Input: None # Output: Prints the letter E. NUM_ACROSS = 3 # Number of asterisks to print across NUM_DOWN = 5 # Number of asterisks to print down. # Write a loop to control the number of rows # Write a loop to control the number of columns # Decide when to priest an asterisk in every column # Decide when to print asterisk in every column. print(\"*\") # Decide when to print a space in column 1. print(\"*\") Decide when to print a space instead of an asterisk print(\" \") # Figure out where to place this statement that prints a newline. print(\"\ \")
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
NUM_ACROSS = 3
NUM_DOWN = 5
for i in range(1,NUM_DOWN+1):
for j in range(1,NUM_ACROSS+1):
if(i==1 or i==3 or i==5): #if the row is first, third or fifth then print NUM_ACROSS *\'s
print(\"*\", end=\'\'),
elif(j==1): #if the column j is 1, then print * for any i. This is for first column
print(\"*\", end=\'\')
elif((i==2 or i==4) and (j!=1)): #if the row is second or fourth, and column other than 1, then print space
print(\" \", end=\'\')
print(\"\ \")
--------------------------------
OUTPUT:
