-3

School assignment asking you to enter name and string. Use the name to determine the initials and remove the initials from the string. Print the original name, original string, initials, and resulting string.

I'm not sure how to proceed.

  1. chars doesn't seem to appear in print "Initials are %s..." in the first function

  2. can't figure out how to delete the parameter chars from txt.

Code:

def main():
    global chars, txt, fullName
    fullName = raw_input("Please input your name: ")
    txt = raw_input("Type in any string: ")
    chars = ''
    getInitials(fullName)
    print "Full name is %s. Original String is %s" % (fullName, txt)
    removeChars(txt,chars)
    print "Initials are %s. Resulting string is %s" %(chars, txt)

def getInitials(fullName):
    chars = ''.join(name[0].upper() for name in fullName.split())
    return

def removeChars(txt, chars):
    for char in txt:
        if char in chars:
            txt = txt.replace(char,'')
    return txt


main()
Pang
  • 9,564
  • 146
  • 81
  • 122
paradise
  • 3
  • 2

3 Answers3

0

You are using global incorrectly (it is generally considered bad practice), and if you return something from your functions then completely unnecessary:

def main():
    fullName = raw_input("Please input your name: ")
    txt = raw_input("Type in any string: ")
    chars = getInitials(fullName)
    sv = removeChars(txt, chars)
    print "Full name is %s. Original String is %s" % (fullName, txt)
    print "Initials are %s. Resulting string is %s" % (chars, sv)

def getInitials(fullName):
    chars = ''.join(name[0].upper() for name in fullName.split())
    return chars

def removeChars(txt, chars):
    for char in txt:
        if char in chars:
            txt = txt.replace(char, '')
    return txt 

main()

Though removeChars isn't very efficient because strings are immutable so each replace would create a new string. You could do the same as what you did with getInitials with a conditional e.g.:

def removeChars(txt, chars):
    return ''.join(char for char in txt if char.upper() not in chars)

Note: you probably meant to upper() the comparison

AChampion
  • 29,683
  • 4
  • 59
  • 75
0

You are missing a few important things:

  • In getInitials(), you are are returning None, not your chars variable. Change your return statement to be return chars

  • In your main function, you are calling getInitials, but not utilizing a return value. Since removeChars is expecting chars and you initially set it to an empty string, nothing is being removed.

Change the following:

getInitials(fullName)

to:

chars = getInitials(fullName)
  • Your removeChars() function is returning a result, but you don't capture that result in main.

Change:

removeChars(txt,chars)

to:

txt = removeChars(txt, chars)

There are a few things to be aware of here:

  • txt = removeChars(txt, chars) is not changing txt, it is assigning a completely new value. It just happens to use the same variable name. In your original post, you had a print statement that utilized sv as a variable name: print "Initials are %s. Resulting string is %s" %(chars, sv), if you assigned the results of removeChars to sv, you'd get the same output and the original string will still be available to you in txt.
  • Remove your global statement.
  • Your original return in getInitials was an empty return statement. This returns None. A function without a return statement also returns None. More details are available in this question
  • Your removeChars is inefficient, since strings are immutable. That means the variable is recreating the string repeatedly. For a small example program like this, though, it works fine.
Community
  • 1
  • 1
Andy
  • 49,085
  • 60
  • 166
  • 233
-1

Do you mean like this?

def main():

    fullName = input("Please input your full name: ")
    txt = input("Type in any string: ")

    initials = '.'.join(name[0].upper() for name in fullName.split())

    print(fullName)
    print (txt)
    print(initials)

main()

You didnt have to -1 me I was just trying to help.