Skip to content
← Back to Community
Rectangle Drawer
Profile icon
ChandlerMorell

The tutorial is within the comments of the code! I encourage you to play around with it!

Voters
Profile icon
a07694
Profile icon
MatthewFrazier
Profile icon
AymericBlassel
Profile icon
MercyMuchangi
Profile icon
nsedler
Profile icon
ash15khng
Profile icon
benjysmith
Profile icon
RAFAELPEREZ4
Profile icon
beneilton
Profile icon
tam_tamtam
Comments
hotnewtop
Profile icon
kaldisberzins

Nice turorial! Easy to understand for a person who barely knows cpp like me.

Profile icon
ChandlerMorell

@kaldisberzins Thank you very much! I am glad you got something out of it!

Profile icon
[deleted]

This is very cool! Nice job! You get my like

Profile icon
ChandlerMorell

@IEATPYTHON Thank you very much! I thought this would be the perfect for loop example for a beginner. I'm clean out of ideas though. I want to make another one.

Profile icon
ChandlerMorell

@IEATPYTHON By the way, I am a huge fan of your Advanced Text Reverser. Haha.

Profile icon
[deleted]

@ChandlerMorell Oh that is not mine it is just a fork. BTW can you please check my tutorial out. Mine is the working memory one

Profile icon
ChandlerMorell

@IEATPYTHON Ayee. Of course!

Profile icon
ChandlerMorell

@IEATPYTHON You should make one of these in Python. After that, eat it, of course. :)

Profile icon
[deleted]

@ChandlerMorell Make what in python?

Profile icon
ChandlerMorell

@IEATPYTHON Rectangle drawer. Or a shape drawer in general.

Profile icon
EdwardWarren

@ChandlerMorell

def enter_width_height():
print()
print("Enter width and height separated by space:")
print()
M=lambda: list(map(int, input().split()))
return M()

def print_square_or_rectangle(width,height):
print()
side=str('* '+' '(width-2)+'')
topbot='* '*width
print(topbot)
for i in range(height-2):
print(side)
print(topbot)
print()

def print_description(f_width,f_height):
if f_width == f_height:
print("This is a ", f_width, "x", f_height, " unit square!")
print("\nArea:", f_width * f_width)
print("Perimeter: ", f_width * 4)
else:
print("This is a ", f_width, "x", f_height, " rectangle!")
print("\nArea:", f_width * f_height)
print("Perimeter: ", 2 * f_width + f_height)

width,height = enter_width_height()
print_square_or_rectangle(width,height)
print_description(width,height)

Profile icon
ChandlerMorell

@EdwardWarren Didn't work for me. I don't know why.. Does is draw a rectangle? Like my C++ program?

Profile icon
XavierDD

Nice tutorial, its easy to read and not to complicated.

Profile icon
ChandlerMorell

@XavierDD Thank you!!

Profile icon
leonekingsley

@XavierDD Wordle This is cool!

Profile icon
2IsAPrimeNumber

If I enter one negative number, the area will be negative, the perimeter will be 0, and no rectangle will be drawn. If I enter two negative numbers, the area will be positive, the perimeter will be (2 * width + 2 * height), and no rectangle will be drawn. If the width contains a decimal, the program will stop, the area will be 0, and the perimeter will be (2 * width rounded down), and no rectangle will be drawn. If the height contains a decimal, the program will round the height down, and continue the repl as if two integers were given.

Profile icon
2IsAPrimeNumber

In sum, the program only runs correctly when two non-negative integers are given.

Profile icon
ChandlerMorell

@2IsAPrimeNumber Yes, that is correct. I used ints instead of doubles, because you can't have a fraction of an asterisk. You also can't have a negative length/width rectangle. I suppose I could make a warning for it. Hope this helps!

Profile icon
2IsAPrimeNumber

@ChandlerMorell I don't know what an "asterisk" is.

Profile icon
ChandlerMorell

@2IsAPrimeNumber This character: "*".