Hi,
How can I print this figure if I have an array like this?
It should be a for-loop but don´t know how.

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

figure:
############. ##
## ##. . . . ##
##. . . ## ####
##. ############
##. . ## ##
####. ######
##. . ## ##
##. ############

Recommended Answers

All 7 Replies

from __future__ import print_function # 

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

[[print("##" if char==0 else ".",end="" if i<len(line)-1 else "\n") for i,char in enumerate(line)]  for line in labyrinth]

Make if statement in midle of for using values as True=1 or False=0

Wooo Slate and tonyjv are really complex

Infact the pseudo of slate is flawless but will a newbie understand the algos.?

I just wrote this as a step by step to the solution. I real life, i advice you to use Slate's example.

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

de=0
for x in labyrinth:
	for y in x:
		de=de+1
		if y == 0:
			print '##',
		else:
			print ".",
		if de == 8:
			print '\n'
			de=0

for fun ;)

We are not supposed to give ready answers. After this your try I must write cleaner version, though. Use 4 space tabs next time.
I prefer not to use line continuation character, but utilize automatic continuation inside parenthesis and brackets:

from __future__ import print_function

labyrinth = [
             [0,0,0,0,0,0,1,0],
             [0,1,0,1,1,1,1,0],
             [0,1,1,1,0,1,0,0],
             [0,1,0,0,0,0,0,0],
             [0,1,1,0,1,1,1,0],
             [0,0,1,1,1,0,0,0],
             [0,1,1,0,1,1,1,0],
             [0,1,0,0,0,0,0,0]]

for line in labyrinth:
    for isopen in line:
        print('  ' if isopen else "##", end='')
    print()
print('-'*40)

# in real life
print('\n'.join(''.join('  ' if isopen else "##" for isopen in line)
                        for line in labyrinth))

wow thanks, now it is Ok!

but tonyjv there is a problem now, if you look at the figure there are some dots, how can I print them just like the figure?

The codes are not there, you maybe must apply one labyrinth solver algorith to find the path, like mine, which solved the labyrinth and when I set the marking of path properly, and gave open places as S and E gave results like this:

The solved labyrinth
############ .##
##  ## .......##
## .....##  ####
## .############
## .. ##      ##
####.     ######
## .. ##      ##
## .############


7.820 ms
Enter to finish
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.