Rachel M.
asked 03/02/23Need help with my code
function 1
def silly_strings(string):
words = []
word = ""
for i in range(len(string)):
if i%2 == 0:
word += string[i].upper()
else:
word += string[i].lower()
if i%2 == 1:
words.append(word)
word = ""
return words
#testing
print(silly_strings("T,E,D,i,s,S,I,L,L,Y")) # should print ['Ted', 'Is', 'Silly']
print(silly_strings("A,L,e,x,i,s,i,s,A,w,e,s,O,M,E")) # should print ['Alexis', 'Is', 'Awesome']
1 Expert Answer
Muhammad Areeb Khan S. answered 03/02/23
Expert Computer Science Professor
It looks like the function silly_strings is supposed to take a string and return a list of words where the first letter of each word is uppercase and the remaining letters are lowercase, alternating between uppercase and lowercase letters for each character.
There are a few issues with the code as written. Here is a corrected version:
Changes made:
Added a check for commas and spaces and ignored them.
Added a check for the end of the string or a comma/space after a lowercase character to know when a new word starts.
Fixed the indentation of the if statement that appends the word to the list.
The output of the corrected code should be as expected.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Rachel M.
I also wrote a second fuction but character limits would let me add it function 2 is def silly_strings(string): words = [] string_list = string.split(',') current_word = "" for char in string: if char == ",": words.append(current_word.capitalize()) current_word = "" else: current_word += char words.append(current_word.capitalize()) return words #testing print(silly_strings("T,E,D,i,s,S,I,L,L,Y")) print( silly_strings("M,Y,t,a,s,A,R,E,r,e,a,l,l,y,A,W,E,S,O,M,E"))03/02/23