Home > Software engineering >  Beginner problems with exercise
Beginner problems with exercise

Time:09-10

I have a problem with this exercise:

#Loop through the letters of the variable 'summer_word' from above #Concatenate the consonants from 'summer_word' and answer with the new word.

summer_word = "music"

Tip: Create a string that contains consonants and check if each letter is in it.

I now how to loop through the letters in the word, but i can't figure out how to take out the consonants.

CodePudding user response:

Let's break it down. You should:

  • hardcode all the consonants into a single string e.g.
consonants = 'qwrt...'
  • loop over the characters of the string stored in the summer_word variable
  • check if the character at hand is included in the consonants string
for letter in summer_word:
    # do stuff...
  • if the letter is in the consonants string, store it into a new string
if letter in consonants:
    # ...

this should be more than enough to get you started :) happy learning

CodePudding user response:

Try this code, might help your case:

summer_word = "music"
consonants = "b, c, d, f, g, j, k, l, m, n, p, q, s, t, v, x, z , h, r, w, y"
new_word = " "

for letter in summer_word:
   if letter in consonants:
        new_word  = letter
print(new_word)

 
  • Related