1

Hi i have a list with True False Values,

visited =[True, False, True, False]

I'm not sure if its possible but id like to use list logic in a while loop when the list is empty or visited has no more False values id like to end the While loop Here is my try it seem to be a infinite loop. And i'm unable to change False Values when i come across one here is my code

class mass:

    def __init__(self):
        visited =[True, False, True, False]
        self.x = 0
        while len([self.x for self.x, values in enumerate(visited) if visited[self.x] == False ]) > 0:
            print(self.x)
            visited[self.x] = True
            print(visited)
            self.x = 0
c = mass()
c

Id like to change the false values as well as stop the loop one the length reaches 0 in the while loop. When i do this in python console i get the output

>>> [x for x, values in enumerate(visited) if visited[x] == False ]
[1, 3]
visited =[True, True, True, True]

Then if i do

>>> visited =[True, True, True, True]
>>> [x for x, values in enumerate(visited) if visited[x] == False ]
[]

so then i tried

>>> print(len([x for x, values in enumerate(visited) if visited[x] == False ]))
0

Which lead me to think i can do

while  len([x for x, values in enumerate(visited) if visited[x] == False ]) > 0

Which loops continuously. Any advice would be excellent thank you

2
  • Your intent is not clear. Do you want the while loop to break when there are no False values in the list? Commented May 19, 2016 at 14:35
  • Yes that's what im trying to do But im unable to change the false values. Which is a problem Commented May 19, 2016 at 14:38

2 Answers 2

4

After you clarified in the comments:

Consider this example:

import random
li = [False, False, False]

while not all(li):
    print('here')
    i = random.randint(0, 2)
    li[i] = True

This code will print 'here' an arbitary number of times, until all the values of li become True.

The key in this answer is the condition of the while loop, while not all(li).

Sign up to request clarification or add additional context in comments.

1 Comment

I thought about using all too, but for a straight solution with no random values I preferred a classic for loop.
1

Your solution seems to be overengineered or the problem description is lacking crucial points.

If you just want to set all values to True you can do the following.

visited = [True, False, True, False]
for index in range(len(visited)):
    visited[index] = True
print(visited)

If you only want to change the value if it is not True do this:

visited = [True, False, True, False]
for index, value in enumerate(visited):
   if not value:    
       visited[index] = True
print(visited)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.