Jay Klope
Jay Klope

Reputation: 145

How to draw "shapes" in python shell?

For example:

Enter the character you want to use to draw your square: *
Enter the width of your square: (insert value here)
Enter the height of your square: (insert value here)

*****
*****
*****

then it should produce an image that is somewhat like the one above.

Upvotes: 0

Views: 3307

Answers (1)

This can be done using single-nested loops, multiplying a string, and the usage of the input function.

char = input("Enter the character you want to use to draw your square: ")
width = int(input("Enter the width of your square: (insert value here): "))
height = int(input("Enter the height of your square: (insert value here): "))

for h in range(0,height):
    print(char * width)

There is an even more efficient way of printing the desired shape in terms of code and time, involving just one line. Replace the loop with this line:

print("\n".join([char * width for _ in range(height)]))

Here is a test run of my program. The first way using a loop took 11908 microseconds, the second took just 4998 microseconds.

Enter the character you want to use to draw your square: %
Enter the width of your square: (insert value here): 10
Enter the height of your square: (insert value here): 3
%%%%%%%%%%
%%%%%%%%%%
%%%%%%%%%%

Upvotes: 4

Related Questions