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.

Recommended Answers

A hint:

ctr=0

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

All 2 Replies

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 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.