I need the function to display a square of numbers. This is the actual function.

``````def draw(n):
for i in range(n+1):
print i*(n)``````

but if i call the function

``def drawBlock(3):``

then the output should be
333
222
111
but i cant understand how to get the numbers displayed three times. I get either just get the range or it multiplies "i" with n?

## All 9 Replies

you need to convert i into string in order to display it n times, and I think a while loop is better here :)

``````def draw( n ):
count = n
while count > 0:
print str( count ) * n
count = count - 1``````

you need to convert i into string in order to display it n times, and I think a while loop is better here :)

``````def draw( n ):
count = n
while count > 0:
print str( count ) * n
count = count - 1``````

sorry but if you dont mind.. could you just explain the "count" thing briefly.. coz if its something which i havnt learned i will have to explain on how i used it.

oki,
count is just a random variable to use in the while loop. Here I'm using it because the specification says that you want to print the numbers starting from n down to 1. Now if you decrease n this means that the number of times a number is printed on the screen is also decreased, therefore you need to decrease another variable but keep the number of times a number is being output. That's what I am using count for. I think it would help a lot if you try to hand-execute the code and see the value for each variable during every iteration :)

hope this helps :)

oki,
count is just a random variable to use in the while loop. Here I'm using it because the specification says that you want to print the numbers starting from n down to 1. Now if you decrease n this means that the number of times a number is printed on the screen is also decreased, therefore you need to decrease another variable but keep the number of times a number is being output. That's what I am using count for. I think it would help a lot if you try to hand-execute the code and see the value for each variable during every iteration :)

hope this helps :)

what does the operation "count-1" do?

subtracts 1 from the value of count:

e.g.

``````>>>count = 3
>>>print count
3
>>>count = count - 1
>>>print count
2
>>>``````

also here it decreases the number which is going to be output by 1

oki,
count is just a random variable to use in the while loop. Here I'm using it because the specification says that you want to print the numbers starting from n down to 1. Now if you decrease n this means that the number of times a number is printed on the screen is also decreased, therefore you need to decrease another variable but keep the number of times a number is being output. That's what I am using count for. I think it would help a lot if you try to hand-execute the code and see the value for each variable during every iteration :)

hope this helps :)

subtracts 1 from the value of count:

e.g.

``````>>>count = 3
>>>print count
3
>>>count = count - 1
>>>print count
2
>>>``````

also here it decreases the number which is going to be output by 1

sorry, but why exactly do we neeed to decrease it by1?? the only thing i can think of is not to include "n" but tht would not make sense i guess..

well you need to decrease it by 1 because the output is
333
222
111

right? if you decrease it by 2 you'll get
333
111

if you don't decrease it at all you'll get an infinite, i.e never ending while loop because count would always be bigger than 0 if it is not decreased. makes sense? :)

well you need to decrease it by 1 because the output is
333
222
111

right? if you decrease it by 2 you'll get
333
111

if you don't decrease it at all you'll get an infinite, i.e never ending while loop because count would always be bigger than 0 if it is not decreased. makes sense? :)

i get all of it nw andoo yes i completely get what yo meant by infinity.. as i tried taking away the count-1 from the program.. thx for the help...

Will be less complex with a for loop and range(start, end, step) starting at the high value, ending at the low value (exclusive) and stepping -1 ...

``````n = 3
for x in range(n, 0, -1):
print str(x)*n

"""result -->
333
222
111
"""``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.