Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0

    need some help on this problem


    I'm suppose to get any word from a user and print out the letters of the words downwards like so :

    F
    u
    n
    c
    t
    i
    o
    n

    and i'm suppose to make it a function in order to do this
    i thought that

    def line(str):
    """ takes in a string and print out it one by one."""
    a=str(raw_input("line: "))
    return a

    would be the function, but i don't know what to do next. plz help me out.
  2. #2
  3. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    think about it - if we're given a string with length N, we're going to need to print on N letters, right? now, take a look at the for loop, the len() function for strings, the range() function, and slice notation for strings.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0
    i have this so far

    def line(str):
    """ takes in a string and print out it one by one."""
    a=str(raw_input("line: "))
    return a
    for i in range(0,len(a)):
    print i

    it doesn't even prompt for user to input, and i'm not too sure about my for loop
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0

    Arrow


    now i have this

    a=str(raw_input("enter a word."))
    s=str(a)
    for i in range(0,len(s)):
    print i

    and it prints out the number of letter in the word in a column
    eg, i input good

    0
    1
    2
    3

    how do i change the numbers to the letters of the words
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    Lawrence, Kansas [KU]
    Posts
    1,559
    Rep Power
    15
    considering you actually seem to be trying, this should do what you want.


    Code:
    #your function
    def linedown(str):
        for i in range(0,len(str)):
            print str[i:i+1]
    
    #testing your function
    a=str(raw_input("enter a word."))
    linedown(a)
    you really need to use code tags when posting python code...
  10. #6
  11. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    Originally Posted by cam_k
    now i have this

    a=str(raw_input("enter a word."))
    s=str(a)
    for i in range(0,len(s)):
    print i

    and it prints out the number of letter in the word in a column
    eg, i input good

    0
    1
    2
    3

    how do i change the numbers to the letters of the words
    jacktasia's example should work - remember what I said about slice notation? that's how you convert string indices to the characters themselves.

    Code:
    print str[i:i+1]
    is slice notation for printing the character(s) between indice i and indice i+1 - essentially the (i+1)th character in the string. since range(len(s)) gives us a range of numbers from zero to one less than the number of characters in the string, the slice notation will give us the 1st through the last characters of the string when we loop through it.
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Slightly more concise is :
    Code:
    >> >  def printChr(string): 
    	for char in string: 
    		print char
    grim
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by deltacoder
    jacktasia's example should work - remember what I said about slice notation? that's how you convert string indices to the characters themselves.

    Code:
    print str[i:i+1]
    is slice notation for printing the character(s) between indice i and indice i+1 - essentially the (i+1)th character in the string. since range(len(s)) gives us a range of numbers from zero to one less than the number of characters in the string, the slice notation will give us the 1st through the last characters of the string when we loop through it.
    Actually the indice [i:i + 1] returns the same value as [i], for example:

    Code:
    >>> string = 'string'
    >>> for number in range(len(string)): print string[number: number + 1]
    
    s
    t
    r
    i
    n
    g
    >>> for number in range(len(string)): print string[number]
    
    s
    t
    r
    i
    n
    g
    >>>
    You should also remember that the raw_input() function returns a sting bu default, so there is no need to try and type-cast it with str(). Also note, that if you don't give range() a starting number explicitly then 0 is used.

    Over all though, I would have gone down the same rout as Grim in his example .

    Anyway have fun guys ,

    Mark.
    Last edited by netytan; October 19th, 2004 at 06:29 AM. Reason: Colored Example
    programming language development: www.netytan.com Hula

  16. #9
  17. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    Originally Posted by netytan
    Actually the indice [i:i + 1] returns the same value as [i]
    Yeah, but as it was apparent cam_k didn't know much about either method, it made sense to help him understand the more versatile one so that he could apply it to other problems as well. It's all good in the end though.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0
    thanks a lot u us. i'm really new to python, so i don't really know much of it. I'm really glad to have this forum to help me out on questions that i don't understand
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0
    i got another question
    how do u make the words go like a piramid.
    eg
    >>>enter word: good

    g
    go
    goo
    good
  22. #12
  23. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    a for loop should work for it, just save each letter you come across and print it out also in the next loop
  24. #13
  25. slightly insane code guru
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2004
    Location
    Indianapolis, IN
    Posts
    871
    Rep Power
    18
    this one just about screams slice notation! take a look at the following example:

    Code:
    str = raw_input("Enter a string:")
    for x in range(len(str)):
       print str[:x+1]
  26. #14
  27. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Another example. This one shifts the addition into range() rather than adding it to the indice each time.

    Code:
    >>> def printPyramid(string):
    	for indice in xrange(len(string) + 1):
    		print string[:indice]
    
    		
    >>> prinPyramid(raw_input('Eneter a string to print: '))
    Eneter a string to print: 
    
    >>> prinPyramid(raw_input('Eneter a string to print: '))
    Eneter a string to print: Hello printPyramid
    
    H
    He
    Hel
    Hell
    Hello
    Hello 
    Hello p
    Hello pr
    Hello pri
    Hello prin
    Hello print
    Hello printP
    Hello printPy
    Hello printPyr
    Hello printPyra
    Hello printPyram
    Hello printPyrami
    Hello printPyramid
    >>>
    It's also not the best idea in the world to over ride Pythons build-in functions - spacifically file(), str(), int() etc. - in your own programs.

    That said, if you can garentee that the function won't be used anywhere in the program then go for it . But it's generally seen as a bad practice.

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just thought I'd another small example here. Its the same idea only the pyramid is printed upside down. which should technically fall over . Either way, it's just for fun so we can ignore the physics of this.

    Code:
    >>> string = 'string'
    >>> for slice in range(len(string)):
    	print string[slice:]
    
    	
    string
    tring
    ring
    ing
    ng
    g
    >>>
    Enjoy,

    Mark.
    programming language development: www.netytan.com Hula

Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo