Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    There is a major problem in this code:

    Code:
    def line(str):
    """ takes in a string and print out it one by one."""
    a=str(raw_input("line: "))
    return a
    In the first line, you are saying "the parameter to this function is called 'str'". Then a moment later, you are saying "str(raw_input)" which now will not work, as you have just overwritten the built-in function str() with your parameter.
    Rename the parameter to something else, such as 'word' or 'stringToPrint', etc.

    It is also not the best idea to name variables 'string' as there is a standard library module with that name, which could cause similar problems.



    On a less serious note, since the replies have already given the normal answers, there's now room for Pythongolf, so...

    Code:
    >>> def vert(s): print '\n'.join(list(s))
    ...
    >>> vert("hello")
    h
    e
    l
    l
    o
    
    >>>
    and hey, no-one has mentioned recursion yet!

    Code:
    >>> def pyramid(s, index):
    ... 	if index <= len(s):
    ... 		print s[:index]
    ... 		pyramid(s, index+1)
    
    >>> pyramid("hello", 0)
    h
    he
    hel
    hell
    hello
    and just because

    Code:
    >>> def pyramid_main(s, index, operator):
    ... 	if index >=0 and index <= len(s):
    ... 		print s[:getattr(0, operator)(index)]
    ... 		pyramid_main(s, index+1, operator)
    
    >>> def pyramid(s, direction):
    ... 	if direction == 'up':
    ... 		pyramid_main(s, 0, '__add__')
    ... 	else:
    ... 		pyramid_main(s+' ', 0, '__sub__')
    ... 		
    >>> pyramid("hello", "up")
    
    h
    he
    hel
    hell
    hello
    >>> pyramid("hello", "down")
    
    hello
    hell
    hel
    he
    h
    
    >>>
  2. #17
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Well let's not forget LIST COMPREHENSION shock horror
    Code:
    def oneper(aString): 
        print ''.join([x+'\n' for x in aString])
    
    def triangle(aString): 
        print ''.join([aString[: x+1]+'\n' for x in range(len(aString))])
    
    oneper("hello")
    triangle("hello")


    Last edited by Grim Archon; October 20th, 2004 at 11:13 AM.
  4. #18
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by sfb
    ...It is also not the best idea to name variables 'string' as there is a standard library module with that name, which could cause similar problems...
    Hey sbf,

    Actually the code will work fine, since str() is only being overwritten inside the line() function in the local namespace. I agree with you of course: as I already mentioned in my last post, this is a bad idea . Anyway, just to prove it will work:

    Code:
    >>> def line(str):
    ...     print str
    ... 
    >>> a = str(raw_input('line: '))
    line: This will work
    >>> 
    >>> a
    'This will work'
    >>>
    As for the string module, where I also agree with your point here I havn't seen it used much at all since mid Python 2.2 and its use steadily declining as we head into 2.4, probably because its easy enough to use the built-in str() class/type so most new comers never bother looking to closly at string.

    grim. You should just so '\n'.join() instead of implicitly concatenating a new line to the end of the string in the List comprehension:

    Code:
    >>> aString = 'hello'
    >>> print '\n'.join([x for x in aString])
    
    h
    e
    l
    l
    o
    >>> print '\n'.join([aString[: x] for x in range(len(aString) + 1)])
    
    h
    he
    hel
    hell
    hello
    >>>
    Never thought of recursion . D'oh.

    Mark.
    Last edited by netytan; October 20th, 2004 at 02:24 PM.
    programming language development: www.netytan.com Hula

  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Originally Posted by netytan
    Actually the code will work fine, since str() is only being overwritten inside the line() function in the local namespace.
    Anyway, just to prove it will work:
    Hi. I guess that depends how you read the code, since it has no indentation it's a bit undecided, but as I read it you get this:

    Code:
    >>> def line(str):
    ... 	a = str(raw_input("line: "))
    ... 	
    >>> line("hello")
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "<interactive input>", line 2, in line
    TypeError: 'str' object is not callable
    >>>
    Still, your example works, so, er, use the indentation you mean, cam_k.

    I agree with you of course: as I already mentioned in my last post, this is a bad idea .
    *cough* Oops, so you did!
    I missed it the first time through :-o
  8. #20
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You win . I read it as being outside the function , probably because when I do a docstring I do it like this:

    Code:
    def functionName(*args, **kwds):
    
        """
        Docstring/Function description...
        """
        ...
    I'll go and make an apointment to get my eyes checked tomorrow .
    programming language development: www.netytan.com Hula

  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    13
    Rep Power
    0
    man, u guys do know ur python, thanks for the help, i've learned a lot from u guys. once again, thanks a lot
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo