#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0

    Some python help


    Hello folks!

    I'll go straight to my problem, I'm trying to program so that the program will ask the user to write some words and the output should be like this. I.e. I write "Brant vag", the result should come out like this ::

    Code:
    b
     r
      a
       n
        t
         
          v
           a
            g
    This is what I've done so far::

    Code:
    print "Det stupar har!"
    textrad = raw_input("")
    
    a = len(textrad)
    
    for tecken in textrad:
        if tecken in "abcdefghijklmnopqrstuvwxyz":
           print textrad
    
    
    print ""
    
    exit = raw_input("Press Enter To Exit")
    I know I look kinda amatuerish but with your help, I can make some progress with my understanding of python language.

    Thank you,
    Z
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    I would do something like this:
    Code:
    textrad = raw_input("")
    
    for i in range(len(textrad)):
        print " " * i, textrad[i]
    Regards, mawe
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0
    Originally Posted by mawe
    Hi!

    I would do something like this:
    Code:
    textrad = raw_input("")
    
    for i in range(len(textrad)):
        print " " * i, textrad[i]
    Regards, mawe

    Wow it actually works Thanks mate! But I want to understand what in in the world just happened .
    I had done the "for i in range(a):" before.. but

    Code:
    print " " * i, textrad[i]
    ? I don't understand, space times 'i', textrad[i] ? What does i mean in the coding?

    *edited*

    Ohh now I understand the " print " " * i " but textrad[i] is still somethign I dont understand?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Sorry, I should have explained it a bit. My English is poor, so I just post some code to do it
    Code:
    >>> print "a" * 3
    aaa
    >>> text = "hello"
    >>> print text[0]
    h
    >>> print text[1]
    e
    Ok, now lets try this in a loop:
    Code:
    >>> for i in range(len(text)):
    .  .  .      print i, text[i]
    0 h
    1 e
    2 l
    3 l
    4 o
    Ok, back to my snippet,
    Code:
    for i in range(len(text)):
        print " " * i, text[i]
    The first round in our loop does this
    Code:
    print " " * 0, text[0]
    So it prints no space, and the first letter of the text.
    Second round:
    Code:
    print " " * 1, text[1]
    prints one space and the second letter of the text, and so on.

    Hope this makes it a bit clearer
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    while 1:
        text_input = raw_input('')
        for ichar in range(len(text_input)):
            print (' ' * ichar) + text_input[ichar]
    You could also do it this way, while this is practically the same thing. Using the while loop will allow you to keep on typing messages.

    The first line reads user input into the variable text_input.
    The second line starts a for loop, it says:
    for every character in the variable text_input, it will output text.
    The third line prints a blank space, multiplied by the current n'th (the n in nth can be any number, for instance, if you entered in, hello world, the 4'th number would be the character o). After the space that the program outputs, it then prints to the screen the n'th character. So once the loop starts, the n'th character would be h (we are guessing you typed in hello world), the 2nd number would be e, and so on. the variable ichar in the for loop equals a number. So text_input[ichar] would equal whatever character in text_input that corresponds with the ichar number. If ichar was 6, then it would prints the 6th character in text_input. Hope this helps.
    Last edited by †Yegg†; June 23rd, 2005 at 10:54 AM.

IMN logo majestic logo threadwatch logo seochat tools logo