Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > nested loops

Reply
Thread Tools

nested loops

 
 
leonardo
Guest
Posts: n/a
 
      02-25-2013
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!


 
Reply With Quote
 
 
 
 
Anssi Saari
Guest
Posts: n/a
 
      02-26-2013
leonardo <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
K. Elo
Guest
Posts: n/a
 
      02-27-2013
Hi!

> leonardo <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
nested loops and conditional statements Porthos XML 3 02-07-2005 08:47 PM
dynamic number of nested loops Allan Bruce Java 5 07-03-2004 09:12 PM
break or continue out of nested loops viza C Programming 5 07-17-2003 04:04 AM
compacting similar nested for loops SplaTTer C++ 2 07-02-2003 09:16 PM



Advertisments