Adblock breaks this site

An initials problem (python)

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

  1. kmjt

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

    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449
    An initials problem (python)

    done..
     
  2. plzmike

    plzmike Newcomer

    Joined:
    Nov 13, 2010
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  3. kmjt

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

    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449
    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.
     
  4. plzmike

    plzmike Newcomer

    Joined:
    Nov 13, 2010
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0
    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() + "."
     
  5. kmjt

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

    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449
    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?
     
  6. plzmike

    plzmike Newcomer

    Joined:
    Nov 13, 2010
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0
    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.
     
  7. kmjt

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

    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449
    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. >


 
 
Adblock breaks this site