0

I currently have a populated list full of nodes in a path from one node to another. I need to stylize the printing of the path as such:

node -> node -> node -> node -> node -> node 

I currently have this code to do so:

 if len(path) %4 == 0: for i in range(0, len(path)): if i+1 % 4 == 0: print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ') print_queue = [] else: print_queue.append(path[i]) if len(path) %4 == 1: path.append(' ') for i in range(0, len(path)): if path[i] == ' ': break if i+1 % 4 == 0: print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ') print_queue = [] else: print_queue.append(path[i]) if len(path) %4 == 2: path.append(' ') for i in range(0, len(path)): if path[i] == ' ': break if i+1 % 4 == 0: print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ') print_queue = [] else: print_queue.append(path[i]) if len(path) %4 == 3: path.append(' ') for i in range(0, len(path)): if path[i] == ' ': break if i+1 % 4 == 0: print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]+' -> '+print_queue[3]+' -> ') print_queue = [] else: print_queue.append(path[i]) if print_queue != []: if len(print_queue) == 1: print(print_queue[0]) if len(print_queue) == 2: print(print_queue[0]+' -> '+print_queue[1]) if len(print_queue) == 3: print(print_queue[0]+' -> '+print_queue[1]+' -> '+print_queue[2]) 

But the path is not being printed. The path list is populated, but the program does not give any output.

What do I need to change in order for this path to print?

1 Answer 1

1

It looks like you want to print each element in the path with " -> " between them.

print(" -> ".join(path)) 

should print them that way.

If you want a " -> " on the end too, just add one on the end in the print statement

print(" -> ".join(path) + " -> ") 

so a path of ["a", "b", "c"] will print a -> b -> c ->

If you want it to break on multiples of 4 nodes you can break the array into chunks of 4 first

path = ["a", "b", "c", "d", "e"] print(" -> \n".join([" -> ".join(path[i:i+4]) for i in range(0, len(path), 4)])) 

which will print

a -> b -> c -> d -> e 
Sign up to request clarification or add additional context in comments.

2 Comments

The only problem with this method is that it prints a -> b -> c -> d -> e-> if there are more than four items in the list. In the correct implementation, e would be on a new line on its own. I figured out the problem with mine however. I simply needed to change i % 4 checks with len(print_queue) = 4 checks
Oh Sorry, I didn't see that. I just updated my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.