# nested loops

Discussion in 'Python' started by leonardo, Feb 25, 2013.

1. ### leonardoGuest

hi everyone,

i have the following program:

import time
count_timer = int(raw_input('how many seconds?: '))
for i in range(count_timer, 0, -1):
print i
time.sleep(1)
print 'blast off!'

this is the result:

how many seconds?: 5
5
4
3
2
1
blast off!

how can i have it print a row of stars beside each number, like this?:

how many seconds?: 5
5 * * * * *
4 * * * *
3 * * *
2 * *
1 *
blast off!

leonardo, Feb 25, 2013

2. ### Anssi SaariGuest

leonardo <> writes:

> how can i have it print a row of stars beside each number, like this?:
>
> how many seconds?: 5
> 5 * * * * *
> 4 * * * *
> 3 * * *
> 2 * *
> 1 *
> blast off!

You could use the repetition operator * since you have the number of
repetitions needed in i. Alternatively, considering the subject, you'd
just add another for loop to print the stars and spaces.

Anssi Saari, Feb 26, 2013

3. ### K. EloGuest

Hi!

> leonardo <> writes:
>
>> how can i have it print a row of stars beside each number, like this?:
>>
>> how many seconds?: 5
>> 5 * * * * *
>> 4 * * * *
>> 3 * * *
>> 2 * *
>> 1 *
>> blast off!

--- snip ---
sec = int(input("How many seconds? "))
for i in range(0,sec):
print str(sec-i)+":"+" *"*(sec-i)
print "blast off!"
--- snip ---

Please note: the value for the upper bound is not included in the range.
In my example, the actual range is from 0 to 4.

HTH,
Kimmo

K. Elo, Feb 27, 2013