An initials problem (python)

Discussion in 'Programming General' started by kmjt, Nov 15, 2010.

An initials problem (python)
  1. Unread #1 - Nov 15, 2010 at 10:56 AM
  2. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    An initials problem (python)

    done..
     
  3. Unread #2 - Nov 15, 2010 at 8:56 PM
  4. plzmike
    Joined:
    Nov 13, 2010
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    plzmike Newcomer

    An initials problem (python)

    Strings are like arrays of characters in most languages and you can access characters like an array.

    To get the first character do the_initials[0][0]


    EXAMPLE:
    >>> hi = ("hello","test")
    >>> print hi
    ('hello', 'test')
    >>> print hi[1]
    test
    >>> print hi[1][0]
    t
     
  5. Unread #3 - Nov 15, 2010 at 9:09 PM
  6. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    An initials problem (python)

    I've been working on the problem all day and have made quite a bit of progress. I updated my first post with a new question aha.
     
  7. Unread #4 - Nov 15, 2010 at 9:21 PM
  8. plzmike
    Joined:
    Nov 13, 2010
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    plzmike Newcomer

    An initials problem (python)

    Theres a built in method called Title that will capitalize the last name properly.

    just do the_initials[2].title() or whatever your last name vairiable is called.

    http://docs.python.org/library/stdtypes.html#str.title


    Just so you know there are much simpler ways to make the first initial. you could do something like

    second_initial = the_initials[1][0].upper() + "."
     
  9. Unread #5 - Nov 15, 2010 at 9:35 PM
  10. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    An initials problem (python)

    Thanks! How would I have my initial() function return my new single string back to the main() function for printing? Also before you told me about second_initial = the_initials[1][0].upper() + "." I made a few changes, will that do to? Anything else you see wrong with my code or that I could make simpler?
     
  11. Unread #6 - Nov 15, 2010 at 9:50 PM
  12. plzmike
    Joined:
    Nov 13, 2010
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    plzmike Newcomer

    An initials problem (python)

    Change
    print "Nice to meet you:", first_initial, second_initial, the_initials[2].title()
    to
    return first_initial + " " + second_initial + the_initials[2].title()

    and in your main function:

    initials(full_name)
    to
    print "Nice to meet you:", initials(full_name)

    and yes
    first_initial = the_initials[0][0].upper() + "."
    second_initial = the_initials[1][0].upper() + "."
    should do the same thing as those 26 if statements.
     
  13. Unread #7 - Nov 15, 2010 at 10:07 PM
  14. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    An initials problem (python)

    Does this look perfect?

    Code:
    def main():
        full_name = raw_input("Please enter your full name (first, middle and last name with a space in between each name): ")
        print "Nice to meet you:", initials(full_name)
    
    
    def initials(full_name):
        the_initials = full_name.split(" ")
    
        first_initial = the_initials[0][0].upper() + "."
        second_initial = the_initials[1][0].upper() + "."
           
    
        return first_initial + " " + second_initial + " " + the_initials[2].title()
    
    
    main()
    
    Didn't add the notes yet.
     
< Really need help, I will Pay! | MySql In a server. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site