Kayla
Kayla

Reputation: 11

how do i print a rectangle with numbers?

i need help making a number rectangle. the width and length are whatever the user inputs but idk how to make the output. it needs to look like this for example: width = 6, length = 4

654321
654321
654321
654321

this is what i have:

def drawnumberrectangle():
    height = getinteger("Enter a height: ")
    width = getinteger("Enter a width: ")
    i = 0
    j = 0
    while i < height:
        while j < width:
            print()
            j = j + 1
        print("")
        j = 0
        i = i + 1

i need something in the print function

Upvotes: 0

Views: 2494

Answers (7)

Keelan Pool
Keelan Pool

Reputation: 177

This looks like a homework problem so I'm not going to give too much away. I would suggest you start with a counted loop (for loop) as this will simplify your code, and use the join() method

height = getinteger("Enter a height: ")
width = getinteger("Enter a width: ")

for i in range(height):
    print(''.join([str(j) for j in range(width,1,-1)]))

To explain these constructs, the join() method joins the argument passed using the string it is called on as the separator e.g. if we wanted to separate the numbers with commas we would use ','.join(numberList). The second thing you'll see if the list comprehension - this creates a list given a condition. In this case, we want a list of all the numbers that are from a to b, with a step of -1 (counting down) so we use range(6,1,-1) and we want those numbers as a string, so we do a type conversion using str(j)

Upvotes: 0

Cohan
Cohan

Reputation: 4544

Welcome to python. Here we have a few funky constructs that save a lot of typing. As you can see, this can be done in far fewer lines than some other languages.

Then range() function will provide you a list of integers. In the line that sets the height, I pass it how many lines we're going to need. In the width, you can see I specified range(1, width +1). This is because range() by default starts at 0, but we needed to actually print "1".

The next trick is the list comprehension [str(w) for w in range(1, width+1)]. This will create a list of characters. But since we're going to print them, I convert them to a string. This is because ''.join() will group them all into one nice string.

The final trick is the part where you see [::-1] which is taking a slice of the list. There are three arguments, start, stop, and step, which are separated by the colons. Blanks mean start at the beginning and end at the end. The step of -1 means do everything backwards.

def rectangle(width, height):

    for h in range(height):
        print(''.join([str(w) for w in range(1, width+1)][::-1]))

rectangle(6, 4)
# 654321
# 654321
# 654321
# 654321

Upvotes: 0

enzo
enzo

Reputation: 11496

Note that, for every line, the numbers goes from width to 1.

''.join([str(x) for x in range(width, 0, -1)])

Explanation

  • range(width, 0, -1) generates a iterable of numbers going from width down to 1.

  • [str(x) for x in range(width, 0, -1)] turns the previous range into a string list.

  • ''.join([str(x) for x in range(width, 0, -1)]) joins the strings of that list in a single string.

Also note that this line repeats length times.

for i in range(length):
    print(''.join([str(i) for i in range(width, 0, -1)]))

Here is your code.

Upvotes: 1

crimson_penguin
crimson_penguin

Reputation: 2778

If you print every number individually, you'll get a line break after each one. Instead, you could construct a line string, and then print that:

def drawnumberrectangle():
    height = getinteger("Enter a height: ")
    width = getinteger("Enter a width: ")
    for i in range(height):
        line = ''
        for j in range(width):
            line += '{}'.format(width - j)
        print(line)

I also changed it to use for in loops and range, which is more Pythonic.

Upvotes: 0

Aaron de Windt
Aaron de Windt

Reputation: 17708

print(width - j, end="") should do it. What this does is print out width - j on each iteration, so it will start at the width (since j=0 at the beginning) and go down to one (since it will be width-1 at the last iteration).

end="" tells print(...) to add an empty string at the end instead of a new line character so that it prints the next character at the same line. Your print("") further down will print out the new line once it's done printing the numbers.

def drawnumberrectangle():
    height = getinteger("Enter a height: ")
    width = getinteger("Enter a width: ")
    i = 0
    j = 0
    while i < height:
        while j < width:
            print(width - j, end="")
            j = j + 1
        print("")
        j = 0
        i = i + 1

Upvotes: 0

razdi
razdi

Reputation: 1440

height = input("Enter a height: ")
width = input("Enter a width: ")
i = int(height)
while i > 0:
    j=int(width)
    while j > 0:
        print(j, end="")
        j = j - 1
    print("")
    i = i - 1

Upvotes: 0

waynelpu
waynelpu

Reputation: 435

Try to use your counter

def drawnumberrectangle():
    height = getinteger("Enter a height: ")
    width = getinteger("Enter a width: ")
    i = 0
    j = 0
    while i < height:
        while j < width:
            print(width-j)
            j = j + 1
        print("")
        j = 0
        i = i + 1

Upvotes: 0

Related Questions