3 Replies - 203 Views - Last Post: 01 July 2019 - 04:39 AM Rate Topic: -----

#1 CodaYoda   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 19

For loop - next word detector

Posted 30 June 2019 - 12:00 PM

Using a for loop I am trying to detect the next word in the list by using a count based system. However, I am having issues with the boolean statements working. The output should be as follows:

b
a
l
l
------Next Word-----

t
a
l
l

------Next Word-----

w
a
l
l

However this ceases to be the case. Any ideas on resolving this issue?

listb = ['ball', 'tall', 'wall']

EndofWord = False
counter = 0

for i in range(len(listb)):
  counter+=1 
  for y in range(4):
     print(listb[i][y])
     if(counter == 4): #once we have hit the last letter indicate that end of word is true
      EndOfword = True
    

      if(EndOfword == True): #once end of word is true perform the following...
        print("\n------Next Word-----\n)")
        EndOfword = False #reset indicator




Is This A Good Question/Topic? 0
  • +

Replies To: For loop - next word detector

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15226
  • View blog
  • Posts: 60,944
  • Joined: 12-June 08

Re: For loop - next word detector

Posted 30 June 2019 - 12:59 PM

So a few things.

First make sure your spacing is correct for loops, ifs, and bodies of both.

Second - make sure the casing of line 2 matches elsewhere.

Third - if you are going in a range with line 8, why do you need the counter variable? Wouldn't it make sense to just print the 'next word' AFTER that inner for loop is done?

Fourth line 15 has an extra closing parenthesis you should remove.
Was This Post Helpful? 0
  • +
  • -

#3 CodaYoda   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 19

Re: For loop - next word detector

Posted 01 July 2019 - 03:03 AM

View Postmodi123_1, on 30 June 2019 - 12:59 PM, said:

Third - if you are going in a range with line 8, why do you need the counter variable? Wouldn't it make sense to just print the 'next word' AFTER that inner for loop is done?


Thank you for the reply. The issue with that is that I would get only the single characters seperated, thus making it look like this.


------Next Word-----

b

------Next Word-----

a

------Next Word-----

t

------Next Word-----

a

------Next Word-----

w

------Next Word-----

a



as opposed to the previous desired specified output. Furthermore, in my full program, there needs to be a way of indicating the end of each word so that a function can be performed.
Was This Post Helpful? 0
  • +
  • -

#4 CodaYoda   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-June 19

Re: For loop - next word detector

Posted 01 July 2019 - 04:39 AM

Hi I worked it out

listb = ['wall', 'ball', 'tall']
for x in listb:
  for y in x:
    print(y)
    print('------------------------')


thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1