While loop - print several times but on 1 line.

D

Danny

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.
 
D

Diez B. Roggisch

Danny said:
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
 
S

Stefan Neumann

Danny said:
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/gltz1ptagYRAlSdAJ4pEasMp8qW0VUelWVQiPJpggAaOACfb2jd
+1+1zViIcJJga+ywji8gm8I=
=8DLC
-----END PGP SIGNATURE-----
 
D

Danny

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.
 
C

Claudio Grondi

Danny said:
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
 
D

Danny

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.
 
F

Fredrik Lundh

Danny said:
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>
 
F

Fredrik Lundh

Danny said:
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?
.... print "text"
....
text
text
text
text
text.... print "text",
....
text text text text text.... sys.stdout.write("text")
....
texttexttexttexttext
oh well.

</F>
 
S

Sion Arrowsmith

Danny said:
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.html#SECTION005200000000000000000
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.html#SECTION009100000000000000000
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.
 
J

Jeffrey Schwab

Danny said:
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.


Looping over indexes is kinda unpythonic in its own right. Is there
something magical about the number 5?

for e in t:
print a[e],
 
B

bruno at modulix

Danny said:
I think I should paste some of the programs code a little more of what I
want...
probably...

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).

It might be important...
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.

If I understand correctly, you have

- a dict 'a' which may look like this:

a = {'a': 1,
'b' : 2,
'c' : 3,
#etc
'z' : 26
}

that is, keys of this dict are one-letter-strings (we dont actually care
what the real values are, enough to know that it's what you want to
print out).

- a string 't' which may look like this :
t = 'abcdefghijklmnopqrstuvwxyz'

And you want to print someting like:
12345

(that is, the dict values which have one of the 5 fisrt letters of t as
keys)

Is that what you actually want ?
 
B

bruno at modulix

Jeffrey said:
Danny said:
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.



Looping over indexes is kinda unpythonic in its own right. Is there
something magical about the number 5?

for e in t:
print a[e],

Or if you want to just iterate over a part of t:
start = 0
end = 6 # end is not included
for e in t[start:end]:
print a[e]*
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top