While loop - print several times but on 1 line.

Discussion in 'Python' started by Danny, Jan 26, 2006.

  1. Danny

    Danny Guest

    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.
     
    Danny, Jan 26, 2006
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Jan 26, 2006
    #2
    1. Advertising

  3. 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/gltz1ptagYRAlSdAJ4pEasMp8qW0VUelWVQiPJpggAaOACfb2jd
    +1+1zViIcJJga+ywji8gm8I=
    =8DLC
    -----END PGP SIGNATURE-----
     
    Stefan Neumann, Jan 26, 2006
    #3
  4. Danny wrote:
    > <snip>


    As a shortcut:

    print "text"*5

    --- Heiko.
     
    Heiko Wundram, Jan 26, 2006
    #4
  5. Danny

    Danny Guest

    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.
     
    Danny, Jan 26, 2006
    #5
  6. 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
     
    Claudio Grondi, Jan 26, 2006
    #6
  7. Danny

    Danny Guest

    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.
     
    Danny, Jan 26, 2006
    #7
  8. 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>
     
    Fredrik Lundh, Jan 26, 2006
    #8
  9. 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>
     
    Fredrik Lundh, Jan 26, 2006
    #9
  10. 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.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.

    --
    \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
     
    Sion Arrowsmith, Jan 26, 2006
    #10
  11. 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.



    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],
     
    Jeffrey Schwab, Jan 26, 2006
    #11
  12. Danny wrote:
    > 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 ?



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno at modulix, Jan 26, 2006
    #12
  13. Jeffrey Schwab wrote:
    > 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.

    >
    >
    >
    > 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]*



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno at modulix, Jan 26, 2006
    #13
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,274
    Tim Slattery
    Mar 30, 2005
  2. djskrill
    Replies:
    9
    Views:
    713
    djskrill
    Oct 1, 2003
  3. Uday Bidkar
    Replies:
    4
    Views:
    509
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    191
    Max Lapshin
    Mar 16, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page