Question

In: Computer Science

Python 3 A program will be written that outputs various geometric shapes, rendered in characters, line-by-line...

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:

  • Square - Done, look at the code.
  • Lower-left Triangle - Like the square but each row has a different length. The length of each row increases by 1 each row.
  • Diagonal - Like the lower-left Triangle but output a space instead of the drawing character, then output a single drawing character after the spaces.
  • Upper-right triangle - Like the diagonal but instead of outputting a single character output a decreasing number of the drawing character.
    Two inner loops will be necessary, one to output spaces followed by one to output the drawing character.
  • Reverse Diagonal - The reverse of the previous diagonal, upper-right to lower-left.
  • Lower-right triangle - Like the just previous diagonal but instead of outputting a single character output an increasing number of the drawing character.
    Two inner loops will be necessary, one to output spaces followed by one to output the drawing character.

3. Some sample outputs:

  • Size 8, drawing character 'o'
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 
  • Size 14, drawing character '+'
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.

  1. Use the condition of the inner while loop to control the output for each line.
    The experiments use this approach.
  2. Run the inner while loop through the value of size and put an if statement in the inner loop to control the output for each line.

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:

  1. A prompt and input for the size.
  2. A prompt and input for a "drawing character".
    This character will be used to draw the shapes.
  3. A square will be displayed.
  4. A triangle will be displayed oriented to the "lower-left".
  5. A diagonal will be displayed.
  6. Another triangle will be displayed oriented to the "upper-right".
  7. A reverse diagonal will be displayed.
  8. A third triangle will be displayed oriented to the "lower-right".
  9. An additional figure will be displayed.

See the sample output to see what each figure looks like.

Some possibilities for the additional figure:

  • An isoceles triangle
  • Some other triangle
  • A trapeziod
  • An X
  • A diamond
  • Something else of your own devising.

Disallowed additional figures:

  • The additional figure can not be a single character.
  • The additional figure can not be a vertical line.
  • The additional figure can not be a horizontal line.
  • The additional figure can not be a rectangle.

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.

Solutions

Expert Solution

# 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")

Related Solutions

Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). 1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input a. Invalid input should throw a message for the user. Example: Invalid input, please try again 2. Each options should ask users for relevant data....
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
create a file with 1000 alphanumeric ones written one per line. Write a program in python...
create a file with 1000 alphanumeric ones written one per line. Write a program in python that randomly selects 100 of them, holds the index of each alphanumeric, adds the hash of the previous 100 and tries to find a random alphanumeric so that if it adds it to the list of indicators the SHA256 of the whole to have 10 zeros at the end. You start with the original Hash: 1111..111 my solution so far: import random import string...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Modules Assignment pythonWrite a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.Write a separate Python program (in a separate file from you Python module) that imports your above module and uses several of its functions in your program. Make use of the input() and print() functions in this program file. Try to make your program as a real-world application.
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Write a program in python that takes a date as input and outputs the date's season....
Write a program in python that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June...
C++ Write a program that outputs an amortization schedule for a given loan. Must be written...
C++ Write a program that outputs an amortization schedule for a given loan. Must be written using user-defined functions must take at least 1 argument Output number of months, payment, interest to pay, principle to pay, and ending balance
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT