-1

Please explain to me how the variable, "count", automatically associates itself with each index of the string, "Hello!"

greeting = 'Hello!'
count = 0

for letter in greeting:
    count += 1
    if count % 2 == 0:
        print(letter)
    print(letter)

print('done')

Basically, the following questions ask about the amount of times each letter of the string prints. After checking the discussion board, I found out that the logic is that the output is H = [1], e = [2], l = [3], l = [4], o = [5], ! = [6]. The thing is, I don't understand why this happens.

11
  • 3
    What do you mean by "automatically associates"??? It simply counts up from 0. Commented Jun 13, 2017 at 23:24
  • H = [1] ??? What? Again, each iteration of the loop the count variable gets incremented by 1. Simply by adding 1 to it the first line of the loop body. Commented Jun 13, 2017 at 23:29
  • for ltr in greeting[::2]:print(ltr) would print every oher letter ... also ... its not very clear what the point of this program is Commented Jun 13, 2017 at 23:31
  • So, a Python for loop is like a Java for-each loop, if that helps you understand. Commented Jun 13, 2017 at 23:32
  • Could you please edit your question to state what exactly your asking. Your true question is confusing because you are saying things like "automatically associates" and "H = [1]". Commented Jun 13, 2017 at 23:37

2 Answers 2

1

Count does not associate with each index of the string.

'Hello' is a string that is composed of multiple characters at various indices:

`'Hello!'[0] = 'H'`
`'Hello!'[1] = 'e'`
`'Hello!'[2] = 'l'`
`'Hello!'[3] = 'l'`
`'Hello!'[4] = 'o'`
`'Hello!'[5] = '!'`

In the for loop, you are incrementing the variable count each time. Thus, in the first iteration, count=0. In the second iteration, count=1, and so on. Your loop is only checking to see if count is divisible by 2. If it is, then it prints out the letter corresponding to its value a second time. Thus, your code would print out

H
e
e
l
l
l
o
!
!
done
Sign up to request clarification or add additional context in comments.

2 Comments

"Thus, in the first iteration, count=0[...]" - No, that is not correct. He immediately increments count at the start of his for loop. So by the time count is used, its value is 1 not 0. And on the second iteration the value is 2 not 1, ad infinitum.
@ChristianDean That is true, but I was trying to simplify the explanation by explaining the value of count before the loop starts, or at the beginning of the loop. But yes, thank you for the clarification! Because he increments count at the beginning of each loop iteration, it starts with a value of 1 in the first iteration, 2 in the second, and so on.
0

You asked:

Please explain to me how the variable, "count", automatically associates itself with each index of the string, "Hello!"

But in your codes, there's no need to use an if statement. And you should add the index or count to near of the item of the string. Simply the codes should be something like:

greeting = 'Hello!'
count = 0
for item in greeting:
    print("item={}, index={}, count={:d}".format(item,greeting.index(item),count))
    count += 1

This will print out:

item=H, index=0, count=0
item=e, index=1, count=1
item=l, index=2, count=2
item=l, index=2, count=3
item=o, index=4, count=4
item=!, index=5, count=5

With the above codes you can see that the count is automatically associates with the each index of the string "Hello!". However when you set the count value for example to 1, the 1'st index (Index0) string associates with when the count=1 then multiply it's value until the end of the index with the for loop.

In "Hello!" string there is 6 items. The first item index always starts with 0. However in order to print a more beautiful display, like the 'first item, second item, third item...' you can add a count variable or you can use enumerate function like the below examples:

greeting = 'Hello!'
count = 1
for item in greeting:
    print("item={}, index={}, count={:d}".format(item,greeting.index(item),count))
    count += 1

greeting = 'Hello!'
for count,item in enumerate(greeting,1):
    print("item={}, index={}, count={:d}".format(item,greeting.index(item),count)) 

The last two codes will give you the same results which are:

item=H, index=0, count=1
item=e, index=1, count=2
item=l, index=2, count=3
item=l, index=2, count=4
item=o, index=4, count=5
item=!, index=5, count=6

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.