Rachel M.

asked • 03/02/23

Need 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']



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"))
Report

03/02/23

Bradley T.

Hi Rachel, I just wanted to clarify the problem. Is the TEDisSILLY string split into the three words Ted Is Silly because they have alternating cases, as in upper and lower case? Should the second example be A,L,E,X,I,S,i,s,A,W,E,S,O,M,E ? I also wanted to ask what your approach towards the problem is. What is your code supposed to do? I see that you separate index by checking mod 2, and this is one way to access letters only and not the commas of the string, but then your else catches all the commas, and you lowercase it before adding it to word. And then you check if mod 2 == 1, which again catches all the commas. And when you see the comma, you add this "word" variable into words list. And I don't think this is what you want. For your second function, it is a little hard to fix without knowing what your aiming for this code to do. I infer that you want to separate words by case, this may be wrong, but I am unsure how you want to approach this.
Report

03/02/23

1 Expert Answer

By:

Muhammad Areeb Khan S. answered • 03/02/23

Tutor
New to Wyzant

Expert Computer Science Professor

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.