Python Forum
[Help] List of Lists not printing both Cmder/Anaconda?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] List of Lists not printing both Cmder/Anaconda?
#1
Hi Python mentors,

I tried running this code:

import random

M = 'land'
o = 'water'
h = 11 #number of rows
w = 11 #number of columns
matrix = []
world = [
         [o,o,o,o,o,o,o,o,o,o,o],
         [o,o,o,o,M,M,o,o,o,o,o],
         [o,o,o,o,o,o,o,o,M,M,o],
         [o,o,o,M,o,o,o,o,o,M,o],
         [o,o,o,M,o,M,M,o,o,o,o],
         [o,o,o,o,M,M,M,M,o,o,o],
         [o,o,o,M,M,M,M,M,M,M,o],
         [o,o,o,M,M,o,M,M,M,o,o],
         [o,o,o,o,o,o,M,M,o,o,o],
         [o,M,o,o,o,M,o,o,o,o,o],
         [o,o,o,o,o,o,o,o,o,o,o]
        ]

def gen_maze():
    rows = []
    for i in range(w):
        matrix.append([])
        col = []
        for j in range(h):
            # matrix[-1].append(0)
            value = random.choice(['o', 'M'])
            col.append(value)
        rows.append(col)
    return rows

gen_maze()
but it wouldn't run either Cmder or Anaconda Prompt?

It works okay in Jupyter though. Is it because the file ends in .ipynb?

Do you have any idea what's causing this?

Thank you.

==============================================================================
[Image: 00_zpsce01yh8k.png]

[Image: 01_zpsqpwxtmt5.png]
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply
#2
Jupyter works as interactive REPL,then you don't need print().
So if start code in ptpython,try ptpython it works well in cmder.
Why ptpython
E:\div_code
λ ptpython -i ma.py
>>> gen_maze()
[['M', 'M', 'o', 'o', 'M', 'o', 'M', 'M', 'M', 'M', 'o'], ....
So it works when in REPL.

Running normal it do not work.
E:\div_code
λ python ma.py
The fix is simple print(gen_maze())
E:\div_code
λ python ma.py
[['M', 'M', 'M', 'o', 'M', 'o', 'o', 'M', 'o', 'M', 'o'], .....
Reply
#3
Thank you so much, @snippsat! Smile

I followed your instructions and the code is running now but could you please give me some idea how can I make the output look cleaner?

Btw, the only thing that I changed in my code above is print(gen_maze())

See below screenshots.

[Image: 1508-e_zpsdszrbeet.png]

[Image: 1508-f_zpsli2pgq1h.png]
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply
#4
You can use pprint.
from pprint import pprint

pprint(gen_maze())
E:\div_code
λ python ma.py
[['M', 'o', 'o', 'M', 'o', 'o', 'o', 'M', 'o', 'o', 'o'],
 ['o', 'M', 'M', 'o', 'o', 'M', 'o', 'M', 'o', 'o', 'M'],
 ['o', 'M', 'o', 'o', 'o', 'M', 'o', 'o', 'M', 'o', 'M'],
 ['o', 'o', 'o', 'o', 'M', 'M', 'o', 'o', 'o', 'M', 'M'],
 ['o', 'M', 'o', 'M', 'o', 'o', 'M', 'M', 'M', 'M', 'M'],
 ['o', 'o', 'M', 'M', 'M', 'M', 'o', 'M', 'M', 'M', 'M'],
 ['o', 'o', 'o', 'M', 'M', 'M', 'o', 'o', 'o', 'o', 'M'],
 ['M', 'o', 'o', 'M', 'M', 'M', 'M', 'M', 'o', 'M', 'o'],
 ['M', 'o', 'o', 'M', 'o', 'o', 'o', 'o', 'o', 'M', 'M'],
 ['o', 'M', 'o', 'M', 'M', 'o', 'M', 'M', 'o', 'M', 'M'],
 ['M', 'o', 'o', 'o', 'o', 'o', 'o', 'M', 'M', 'M', 'M']]
Reply
#5
Thank you very much for being so generous, @snippsat! Smile


from pprint import pprint
import random

M = 'land'
o = 'water'
h = 11 #number of rows
w = 11 #number of columns
matrix = []
world = [
         [o,o,o,o,o,o,o,o,o,o,o],
         [o,o,o,o,M,M,o,o,o,o,o],
         [o,o,o,o,o,o,o,o,M,M,o],
         [o,o,o,M,o,o,o,o,o,M,o],
         [o,o,o,M,o,M,M,o,o,o,o],
         [o,o,o,o,M,M,M,M,o,o,o],
         [o,o,o,M,M,M,M,M,M,M,o],
         [o,o,o,M,M,o,M,M,M,o,o],
         [o,o,o,o,o,o,M,M,o,o,o],
         [o,M,o,o,o,M,o,o,o,o,o],
         [o,o,o,o,o,o,o,o,o,o,o]
        ]

def gen_maze():
   rows = []
   for i in range(w):
      matrix.append([])
      col = []
      for j in range(h):
         # matrix[-1].append(0)
         value = random.choice(['o','M'])
         col.append(value)
      rows.append(col)
   return rows

pprint(gen_maze())
[Image: 150818_zpshkfc5afh.png]
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,023 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,561 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Printing through list.. again jesse68 2 1,109 Apr-16-2022, 03:24 PM
Last Post: jesse68
  How to efficiently average same entries of lists in a list xquad 5 2,070 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,805 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,034 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 3,353 May-09-2021, 07:49 AM
Last Post: Gribouillis
  help for list printing jip31 8 3,550 May-01-2021, 03:52 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020