Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39939 > unrolled thread
| Started by | leonardo <tampucciolina@libero.it> |
|---|---|
| First post | 2013-02-25 23:46 +0100 |
| Last post | 2013-02-27 09:02 +0200 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
nested loops leonardo <tampucciolina@libero.it> - 2013-02-25 23:46 +0100
Re: nested loops Alister <alister.ware@ntlworld.com> - 2013-02-26 14:36 +0000
Re: nested loops Anssi Saari <as@sci.fi> - 2013-02-26 21:39 +0200
Re: nested loops "K. Elo" <maillists@nic.fi> - 2013-02-27 09:02 +0200
| From | leonardo <tampucciolina@libero.it> |
|---|---|
| Date | 2013-02-25 23:46 +0100 |
| Subject | nested loops |
| Message-ID | <mailman.2537.1361871951.2939.python-list@python.org> |
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!
[toc] | [next] | [standalone]
| From | Alister <alister.ware@ntlworld.com> |
|---|---|
| Date | 2013-02-26 14:36 +0000 |
| Message-ID | <YL3Xs.206878$US3.107180@fx16.fr7> |
| In reply to | #39939 |
On Mon, 25 Feb 2013 23:46:11 +0100, leonardo wrote:
> 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!
you could try simply changing your print statement to
print "%s %s"%(i,"*" * i)
:-)
--
Someone in DAYTON, Ohio is selling USED CARPETS to a SERBO-CROATIAN
[toc] | [prev] | [next] | [standalone]
| From | Anssi Saari <as@sci.fi> |
|---|---|
| Date | 2013-02-26 21:39 +0200 |
| Message-ID | <vg338wirhj5.fsf@coffee.modeemi.fi> |
| In reply to | #39939 |
leonardo <tampucciolina@libero.it> 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.
[toc] | [prev] | [next] | [standalone]
| From | "K. Elo" <maillists@nic.fi> |
|---|---|
| Date | 2013-02-27 09:02 +0200 |
| Message-ID | <mailman.2601.1361959322.2939.python-list@python.org> |
| In reply to | #39998 |
Hi!
> leonardo <tampucciolina@libero.it> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web