Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > While loop - print several times but on 1 line.

Reply
Thread Tools

While loop - print several times but on 1 line.

 
 
Danny
Guest
Posts: n/a
 
      01-26-2006
Hello there.

I'm creating a little text changer in Python. In the program there is a
while loop. The problem is that a while loop will have 1 print statement
and it will loop until it gets to the end of the text.
Example:

num = 5 // Set num to 5
while num >= 1: // loop 5 times.
print """text"""
num = num-1
// end.

The programs output will be:
text
text
(etc)

How could I make this print: texttexttexttexttext?
Ive researched and looked through google and so far I can't find
anything that will help (or revelent for that matter).

Thanks for looking.
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-26-2006
Danny wrote:

> Hello there.
>
> I'm creating a little text changer in Python. In the program there is a
> while loop. The problem is that a while loop will have 1 print statement
> and it will loop until it gets to the end of the text.
> Example:
>
> num = 5 // Set num to 5
> while num >= 1: // loop 5 times.
> print """text"""
> num = num-1
> // end.


Don't use while for that, this is considered unpythonix. Use a for-loop with
xrange instead. If you need the nums in that order, use xrange with a
negative step:

Use sys.stdout.write:

import sys

for i in xrange(num, 0, -1):
sys.stdout.write("text")


Diez
 
Reply With Quote
 
 
 
 
Stefan Neumann
Guest
Posts: n/a
 
      01-26-2006


Danny wrote:
> How could I make this print: texttexttexttexttext?
> Ive researched and looked through google and so far I can't find
> anything that will help (or revelent for that matter).


I am not quite sure, if I simplify the problem but i thought about
something like that:

>>> print "text"*5
texttexttexttexttext

cheers

Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBD2KLV/gltz1ptagYRAlSdAJ4pEasMp8qW0VUelWVQiPJpggAaOACfb2j d
+1+1zViIcJJga+ywji8gm8I=
=8DLC
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Heiko Wundram
Guest
Posts: n/a
 
      01-26-2006
Danny wrote:
> <snip>


As a shortcut:

print "text"*5

--- Heiko.
 
Reply With Quote
 
Danny
Guest
Posts: n/a
 
      01-26-2006
I think I should paste some of the programs code a little more of what I
want...

var = 0
while var <= 5:
print a[t[var]]
var = var +1

a is a dectionary (very big) and t is a string of text. (if that's
important right now).

I'm just trying to make the value of a[t[var]] print on one line if that
makes any sense...
Sorry if I'm not explaining this very well and if my examples aren't
very good, I am trying.
 
Reply With Quote
 
Claudio Grondi
Guest
Posts: n/a
 
      01-26-2006
Danny wrote:
> I think I should paste some of the programs code a little more of what I
> want...
>
> var = 0
> while var <= 5:
> print a[t[var]]
> var = var +1
>
> a is a dectionary (very big) and t is a string of text. (if that's
> important right now).
>
> I'm just trying to make the value of a[t[var]] print on one line if that
> makes any sense...
> Sorry if I'm not explaining this very well and if my examples aren't
> very good, I am trying.

I mean that what you are looking for is:
print a[t[var]],
Notice the last comma in the line above.

Claudio
 
Reply With Quote
 
Danny
Guest
Posts: n/a
 
      01-26-2006
Great! It's been solved.

The line, as Glaudio said has a "," at the end and that makes it go onto
one line, thanks so much man!

var = 0
while <= 5:
print a[t[var]],
var = var +1
prints perfectly, thanks so much guys.
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-26-2006
Danny wrote:

> I think I should paste some of the programs code a little more of what I
> want...
>
> var = 0
> while var <= 5:
> print a[t[var]]
> var = var +1
>
> a is a dectionary (very big) and t is a string of text. (if that's
> important right now).
>
> I'm just trying to make the value of a[t[var]] print on one line if that
> makes any sense...


if you don't want print's behaviour, you can print directly to the
stdout stream:

import sys

for var in range(5):
sys.stdout.write(a[t[var]])

write only accepts strings, so if the dictionary may contain other
stuff, you need to use the str() function to convert the data on
the way out:

for var in range(5):
sys.stdout.write(str(a[t[var]]))

</F>



 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-26-2006
Danny wrote:

> Great! It's been solved.
>
> The line, as Glaudio said has a "," at the end and that makes it go onto
> one line, thanks so much man!
>
> var = 0
> while <= 5:
> print a[t[var]],
> var = var +1
> prints perfectly, thanks so much guys.


if you wanted spaces between the items, why didn't you
say that ?

> How could I make this print: texttexttexttexttext?


>>> for i in range(5):

.... print "text"
....
text
text
text
text
text
>>> for i in range(5):

.... print "text",
....
text text text text text
>>> import sys
>>> for i in range(5):

.... sys.stdout.write("text")
....
texttexttexttexttext
>>>


oh well.

</F>



 
Reply With Quote
 
Sion Arrowsmith
Guest
Posts: n/a
 
      01-26-2006
Danny <> wrote:
>The programs output will be:
>text
>text
>(etc)
>
>How could I make this print: texttexttexttexttext?
>Ive researched and looked through google and so far I can't find
>anything that will help (or revelent for that matter).


I'm kind of surprised this isn't a FAQ (if it's in the FAQs, I
can't find it).

http://docs.python.org/tut/node5.htm...00000000000000
tells you how to use

print "text", # Note the comma. Oh, and the correct comment character.

to get

text text text text text

http://docs.python.org/tut/node9.htm...00000000000000
hints that what you want may be

sys.stdout.write("text")

to get

texttexttexttexttext

Beware that in either case you'll need an additional print at the
end of the loop to get the final newline back.

--
\S -- -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Why is the Constructor called 4 times but the Destructor 5 times? djskrill C++ 9 10-01-2003 07:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57