Question

Python 3: Please follow the respective rubric for the following function definition.

Rectangle Shape You should implement the following functions to print a rectangle shape. • print_rectangle(length, width, fil

Rubric:

[3 points) Prints the rectangle: • Asks correctly for the values allowed. • If the value is incorrect it keeps looping and sh

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

#method to print the rectangle
def print_rectangle(length, width, fillChar, hollow):
    #looping through each row
    for i in range(length):
        #looping through each column
        for j in range(width):
            #if hollow is False, simply printing fillChar
            if not hollow:
                print(fillChar, end='')
            else:
                # otherwise checking if current position is at the edge
                if i==0 or j==0 or i==length-1 or j==width-1:
                    #printing fillChar
                    print(fillChar, end='')
                else:
                    #printing a blank space
                    print(' ', end='')
        #printing line break
        print()


#method to read values from user and print a rectangle
def make_rectangle():
    #initializing length to -1
    length = -1
    #looping until a valid length is entered
    while length < 0 or length > 20:
        #reading and validating length
        length = int(input("Enter a valid length between 1 and 20: "))
        if length < 0 or length > 20:
            print("Invalid length, try again.")
    #doing the same for width
    width=-1
    while width<0 or width>80:
        width=int(input("Enter a valid width between 1 and 80: "))
        if width<0 or width>80:
            print("Invalid width, try again.")
    #reading fill char
    c=input("Enter a fill character: ")
    #reading fill status. if input is 'y' or 'Y', the rectangle will be hollow
    #anything else, rectangle will be filled
    hollow=input("rectangle needs to be hollow? (y/n): ").lower()=='y'
    print() #printing a blank line
    #printing rectangle
    print_rectangle(length, width, c,hollow)


#testing the above method
make_rectangle()

#output 1

Enter a valid length between 1 and 20: -2 Invalid length, try again. Enter a valid length between 1 and 20: 5 Enter a valid w

#output 2

Enter a valid length between 1 and 20: 7 Enter a valid width between 1 and 80: 20 Enter a fill character: 0 rectangle needs t

Add a comment
Know the answer?
Add Answer to:
Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT