1

If I write an input like 'I like apples' then I want the output to be I.L.Apples, but I keep getting I.L.A.Apples. Can anyone help me on removing the 'A.' part?

user_input = input('Enter a sentence: ')
sentence = user_input.split()

a = ""
b = ""

lastword = user_input.split()[-1]

for i in sentence:
  a = a + str(i[0]).upper() + '.'
  b = lastword.title()


print(a+b)
martineau
  • 119,623
  • 25
  • 170
  • 301
rretto
  • 11
  • 2
  • So, let's debug. It seems like something's going wrong in your `for i in sentence` part, as it's hitting every word instead of every word but the last. Can you find a way to not iterate over that last word? I'll give you more hints if you get stuck. (Side notes that are not causing this bug: Pick better variable names than `a` and `b`, and do you need to titlecase that last word for every i in sentence, or just once?) – thumbtackthief Sep 15 '21 at 21:50
  • 1
    @thumbtackthief I figured out what was wrong and now it works well, just had to add [:-1] to my for loop and put the "b" outside. Also changed variable names, but why is it not a good idea to name it as a and b? I thought it would be a simple option for this code since it's very short. – rretto Sep 15 '21 at 23:04
  • Nice! Good question: In general, you want to choose descriptive, clear names for your variables (often harder than it sounds). Although this piece of code will probably not go anywhere beyond this assignment, it's a good habit to get into. If you were to come back to this code in a month, would you remember what `a` and `b` meant? Would someone else on your team know? It's just extra work to figure it out, when you could have named them `abbreviated_words` and `last_word`. Much easier to read! – thumbtackthief Sep 16 '21 at 15:48

1 Answers1

3

This looks like a homework problem, so I don't think I should give you the answer. But some thoughts.

  1. Why are you setting "b" inside your loop, rather than outside it?

  2. You are looping over all the words in the sentence adding their initials. Don't you think you should be looping over all the words in the sentence except the last one?

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22