nested loops

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

  1. leonardo

    leonardo Guest

    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
    #1
    1. Advertising

  2. leonardo

    Anssi Saari Guest

    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
    #2
    1. Advertising

  3. leonardo

    K. Elo Guest

    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
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    dynamic number of nested loops

    Allan Bruce, Jul 1, 2004, in forum: Java
    Replies:
    5
    Views:
    9,721
    Tor Iver Wilhelmsen
    Jul 3, 2004
  2. Porthos
    Replies:
    3
    Views:
    474
    Joris Gillis
    Feb 7, 2005
  3. SplaTTer
    Replies:
    2
    Views:
    369
    SplaTTer
    Jul 2, 2003
  4. viza

    break or continue out of nested loops

    viza, Jul 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    1,003
    sunil
    Jul 17, 2003
  5. Me
    Replies:
    2
    Views:
    229
Loading...

Share This Page