Jan-11-2020, 07:45 AM
Hello,
I am working through some textbook exercises and was given the task:
Write a program that allows a user to type in a phrase and provides the acronym as the output. The acronym should be in uppercase.
I was able to solve it using two different approaches. The first solution works great and was my attempt after deleting everything and coming back to it fresh on the second day.
The second solution works but I stole code from Stack Overflow and don't fully understand it. Specifically, the last line which is using the translate method. Could someone help me digest this method?
Is it somehow looping through the letterString, looking at each character (ord) in the string and returning a 'none' value (i.e. blank) for any of those characters I selected?
Changing the code to add in "bacon" swaps out those characters for bacon.
I know the second solution is ugly but wanted to learn from it anyway. It hasn't 'clicked' so hoping someone can help me think about it another way.
Thanks so much!
I am working through some textbook exercises and was given the task:
Write a program that allows a user to type in a phrase and provides the acronym as the output. The acronym should be in uppercase.
I was able to solve it using two different approaches. The first solution works great and was my attempt after deleting everything and coming back to it fresh on the second day.
The second solution works but I stole code from Stack Overflow and don't fully understand it. Specifically, the last line which is using the translate method. Could someone help me digest this method?
Is it somehow looping through the letterString, looking at each character (ord) in the string and returning a 'none' value (i.e. blank) for any of those characters I selected?
Changing the code to add in "bacon" swaps out those characters for bacon.
print(letterString.translate({ord(i): "bacon" for i in " ',"}))So, where I am struggling is around the syntax
{ord(i): blah blah}How does this syntax know to loop through each character in the string?
I know the second solution is ugly but wanted to learn from it anyway. It hasn't 'clicked' so hoping someone can help me think about it another way.
Thanks so much!
#Allows user to enter a string phrase = str(input("Enter your phrase: ")) #Splits the string by each space (word) #Note: after using a split, this sets the variable type to LIST! split_phrase = phrase.split() #Set an empty string to contain the acronym acronym = str("") #Loop through the items in the LIST of split_phrase #Take the first element, set it as upper case #Add it into the acronym variable for i in split_phrase: first_letter = i[0].upper() acronym = acronym + first_letter print("acronym is: " + acronym)
#User enters the string #Split function divides it based on space user_input = str(input("Please enter your phrase: ")).split() #Creates a list. #Loops through each item (word) in the user_input string. For the 0 index (first character) of each word. Assigns to list firstList = [x[0] for x in user_input] #Creates a new list #For each item in the list, change it to upper and assign it to the variable secondList = [element.upper() for element in firstList] #Creates a string from the list letterString = str(secondList) print(letterString.translate({ord(i): None for i in " ',"}))