[Help] List of Lists not printing both Cmder/Anaconda? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [Help] List of Lists not printing both Cmder/Anaconda? (/thread-12231.html) |
[Help] List of Lists not printing both Cmder/Anaconda? - vanicci - Aug-15-2018 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] RE: [Help] List of Lists not printing both Cmder/Anaconda? - snippsat - Aug-15-2018 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.pyThe fix is simple print(gen_maze()) E:\div_code λ python ma.py [['M', 'M', 'M', 'o', 'M', 'o', 'o', 'M', 'o', 'M', 'o'], ..... RE: [Help] List of Lists not printing both Cmder/Anaconda? - vanicci - Aug-15-2018 Thank you so much, @snippsat! 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] RE: [Help] List of Lists not printing both Cmder/Anaconda? - snippsat - Aug-15-2018 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']] RE: [Solved] List of Lists not printing both Cmder/Anaconda? - vanicci - Aug-15-2018 Thank you very much for being so generous, @snippsat! 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] |