Thread: New With python

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

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    New With python


    Hello There I am new in Python I have an issue with the code below.

    name = raw_input("What is your name?")
    quest = raw_input("What is your quest?")
    color = raw_input("What is your favorite color?")

    print "Ah, so your name is %s"%name," your quest is %s"%quest, \
    "and your favorite color is %s" %color.___ (name, quest, color)

    Then I receive the follow error when answer all the questions
    What is your name? Test
    What is your quest? Test
    What is your favorite color? Test

    Traceback (most recent call last):
    File "python", line 6, in <module>
    AttributeError: 'unicode' object has no attribute '___'

    I am wondering if I should put % after color in the end of the code or not?

    I am sorry if the question is about confused.

    Any help will be appreciated.

    All the best.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    What sort of document did you read that showed that three successive underscores had some particular use?
    Who told you?
    It's just not so.
    Sure, ___ is a valid variable name. No one, no computer assigned such an attribute in your program.

    Here are some ways to format your string. As you'll see, you mixed processing one string at a time with all of the strings at once. With the additional confusion of ___.
    Code:
    name = quest = color = 'test'
    
    
    # here python processes one string at a time.
    # The commas used to separate arguments have
    # lowest precedence in the print statement
    # Each string is formatted independently,
    # then print writes the strings separated by a
    # space character and terminated with new line.
    print "Ah, so your name is %s"%name, " your quest is %s"%quest,\
          "and your favorite color is %s" %color # python2 only
    
    
    
    # Process all of the strings at once.
    print('Ah, so your name is %s  your quest is %s and your favorite color is %s'%(name, quest, color))
    
    # Python joins successive strings.
    # Python also reads parenthesized expressions across multiple lines.
    # Thus we can write shorter lines without using the treacherous backslash line continuation
    print('Ah, so your name is %s  your quest is %s '
          'and your favorite color is %s'
          %(name, quest, color))
    
    
    
    
    # The modern way:
    print('Ah, so your name is {}  your quest is {} and your favorite color is {}'.format(name, quest, color))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Another modern option ...
    Code:
    # get user input ...
    name = raw_input("What is your name? ")
    quest = raw_input("What is your quest? ")
    color = raw_input("What is your favorite color? ")
    
    # create a format string
    sf = '''
    Ah, so your name is {name}
    your quest is {quest}
    and your favorite color is {color}'''
    
    # unpack the local dictionary vars()
    print(sf.format(**vars()))
    
    ''' possible result ...
    What is your name? Fred
    What is your quest? sleep
    What is your favorite color? blue
    
    Ah, so your name is Fred
    your quest is sleep
    and your favorite color is blue
    '''

    Comments on this post

    • b49P23TIvg agrees : Excellent, easier than str.Template which I've hardly bothered to investigate.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo