Learning don’t need to be boring

--Originally published at Welcome to the Heaven

Today I will teach you how to make a words counter and at the same time you will learn how to use the loopfor“.

First I need that you understand how it works this kind of loop. “For” is pretty different from while, because the second one will run the times that the answer be true, and the first one, the important for this post, “for” is going to repeat a certain number of times. 

Okay know we can start.

The program code that you’ll see is from a program that can count a specific letter from your text or count all the words that the input has. (If you want, I can help you to do that this program count a specific words, the only request is that you comment if you want it or I will teach you how to make that the program run with a document with text).

screen-shot-2016-10-27-at-1-07-25-amAs you can see the program have a question that serves to select if you want to count a specific letter or all the words.

We’ll focus in one answer to explain you how it “for” works.screen-shot-2016-10-27-at-1-14-42-amFirst to make the count possible, we define the variables that we going to use.

  -“count” makes that the program start to count in 0.

-“words” is the complete input.

-“word” is the same that words, the only thing that change is that we use <.split()> to     separate the words that have an space between and when we use this function the program can know that a group of letters with space is a word for us.

The loop for in this case makes that the count increment plus one, for a word that find in our variable “word”.

count is going to stop when the code repeat for all the words that the variable has.