In: Computer Science
Python 3
A program will be written that outputs various geometric shapes, rendered in characters, line-by-line using nested loops.
Here is what you need to know:
1- Copy this and don't change the inputs in the provided code:
# Get the size and drawing character from the user size = input('Please enter the size: ') # Validate the input, exit if bad if size.isdigit(): size = int(size) else: print("Exiting, you didn't enter a number:", size) exit(1) # Input the drawing character drawingChar = input('Please enter the drawing character: ') # Output an empty line print() #=============================================================== # Draw a Square row = 1 while row <= size: # Output a single row col = 1 while col <= size: if col <= size: # Output the drawing character print(drawingChar, end=' ') # The next column number col = col + 1 # Output a newline to end the row print() # The next row number row = row + 1 print() #=============================================================== # Draw a Lower-Left Triangle #=============================================================== # Draw a Diagonal #=============================================================== # Draw an Upper-Right Triangle #=============================================================== # Draw a Reverse Diagonal #=============================================================== # Draw a Lower-Right Triangle #=============================================================== # Draw a ...
2. Comments on the shapes:
3. Some sample outputs:
Please enter the size: 8 Please enter the drawing character: o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
Please enter the size: 16 Please enter the drawing character: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
3. Outputting A Row Of characters: (how the code will look like)
- A row (line) of x's can be output with the following code:
# A string comprised of 1 character drawingChar = 'x' col = 1 while col <= size: # Output the drawing character print(drawingChar, end=' ') # The next column number col = col + 1 # Output a newline to end the row print()
4. Outputting a rectangle of characters:
- If the above loop is done repeatedly, that is within another while loop, a square would be output.
# A string comprised of 1 character drawingChar = 'x' row = 1 while row <= size:
# Output a single row col = 1 while col <= size: # Output the drawing character print(drawingChar, end=' ') # The next column number col = col + 1
# Output a newline to end the row print() # The next row number row = row + 1 print()
We have a loop "within" a loop. This is called nested loops.
The first loop is called the outer loop and the loop "within" the outer loop is called the inner loop.
4. How to approach the problem?
There are two approaches you can use for drawing the shapes.
5. FINALLY, some additional notes to keep in mind for the shape program notes:
When the completed Shapes Program is run the following will happen:
See the sample output to see what each figure looks like.
Some possibilities for the additional figure:
Disallowed additional figures:
All figures must use nested loops, that is there will be a loop within a loop, as in the code above.
Do not use string concatenation or string methods.
Implement the shapes in the order above, one after the other in your program file.
# Get the size and drawing character from the user size = input('Please enter the size: ') # Validate the input, exit if bad if size.isdigit(): size = int(size) else: print("Exiting, you didn't enter a number:", size) exit(1) # Input the drawing character drawingChar = input('Please enter the drawing character: ') # Output an empty line print() #=============================================================== # Draw a Square row = 1 while row <= size: # Output a single row col = 1 while col <= size: if col <= size: # Output the drawing character print(drawingChar, end=' ') # The next column number col = col + 1 # Output a newline to end the row print() # The next row number row = row + 1 print() #=============================================================== # Draw a Lower-Left Triangle row = 1 while row <= size: col = 1 while col<=row: print(drawingChar ,end=' ') col = col + 1 row = row + 1 print() print() #=============================================================== # Draw a Diagonal row = 1 while row <= size: col = 1 while col<row: print(" ",end=' ') col = col+1 print(drawingChar) row = row+1 print() #=============================================================== # Draw an Upper-Right Triangle # number of spaces k = size - 1 for i in range(size, -1, -1): for j in range(k, 0, -1): print(end=" ") k = k + 1 for j in range(0, i + 1): print(drawingChar, end=" ") print("") print() #=============================================================== # Draw a Reverse Diagonal row = 1 s = size while row <= size: col = 1 while col<s: print(" ",end=' ') col = col+1 s=s-1 print(drawingChar) row = row+1 print() #=============================================================== # Draw a Lower-Right Triangle k = size - 1 # outer loop to handle number of rows for i in range(0, size): # inner loop to handle number spaces # values changing acc. to requirement for j in range(0, k): print(end=" ") # decrementing k after each loop k = k - 1 # inner loop to handle number of columns # values changing acc. to outer loop for j in range(0, i + 1): # printing stars print("{0} ".format(drawingChar), end="") # ending line after each row print("\r")