Python while question

Asked 2 years ago, Updated 2 years ago, 86 views

Below is the width-first search algorithm code Go back without errors and the answer is "Thom". But in the while door, whilesearch_queue: while search_queue == True: I shouldn't have used it like this What is the difference between the two phrases?

graph = {}

graph["you"] = ["alice", "bob", "claire"]

graph["alice"] = ["peggy"]
graph["bob"] = ["anuj", "peggy"]
graph["claire"] = ["thom", "jonny"]

graph["anuj"] = []
graph["peggy"] = []
graph["thom"] = []
graph["jonny"] = []


from collections import deque

def person_is_seller(name):
    return name[-1] == "m"

def search(name):
    search_queue = deque()
    search_queue += graph["you"]
    # # print(search_queue)
    searched = []

    while search_queue:
        person = search_queue.popleft()
        if not person in searched:
            if person_is_seller(person):
                print(person + " is mango seller!")
                return True
            else:
                search_queue += graph[person]
                searched.append(person)
    return False

search("you")

while-loop python bfs

2022-09-22 14:18

1 Answers

search_queue is a queue object In the if or while conditional clause, search_queue is treated as False when empty If one or more values are included, they are treated as True.

If direct search_queue == True, search_queue is always false because it is not a boolean value.


2022-09-22 14:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.