s = raw_input("Enter a string: ")
a = 0
i = 0
for i in range(len(s)):
    if s[i] == ' ':
        a = a + 1
        print str(a) + '. ' + 'There was a space in position ' + str(i) + '.'

i keep on trying to sub in while instead but it won't compute an answer.

A hint:

ctr=0

while ctr < len(s):
    print ctr, s[ctr]
    ctr += 1

You just have to manually increase i at the end of the loop. And for the for loop you don't have to have i = 0 above it, just say for i in range(len(s)):

s = raw_input("Enter a string: ")
a, i = 0, 0
while i < len(str(s)):
    if s[i] == ' ':
        a += 1
        print str(a) + '. ' + 'There was a space in position ' + str(i) + '.'
    i += 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.