Posts: 22
Threads: 12
Joined: Nov 2019
Nov-17-2019, 10:16 PM
(This post was last modified: Nov-17-2019, 10:24 PM by Reldaing.)
Hi, I'm trying to get familiar with lists. Right now, I have a little Issue. I'm trying to convert this code (kind of sodoku) into a function, who takes in arguments lists . here's my code:
def Hi1():
a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j], end=' ')
print() And this is what I made, I can't understand how array works in python, Thank ya all !
def Hi2(a):
a=[[],[],[],[],[],[],[],[]]
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j], end=' ')
print()
Posts: 479
Threads: 86
Joined: Feb 2018
Nov-17-2019, 10:36 PM
(This post was last modified: Nov-18-2019, 10:51 PM by SheeppOSU.)
(Nov-17-2019, 10:16 PM)Reldaing Wrote: I can't understand how array works in python Imagine an array as a shelf. If I make an array like this for example array = [1, 2, 3, 4] you can imagine it as a shelf with number from left to right. If I want to call upon a certain part of the array, I use numbers. So if I call column 0 array[0] then it will return the 1. If I put an array, inside an array, then it's basically shelves in shelves. I use numbers to call upon the shelf I want, then I use numbers to call from things in that shelf. Here's an example colors = [[0, 0, 0], [255, 255, 255], [200, 10, 10]] Here is a list of colors. The colors are numbers stored in arrays. So to call the first color (black), I do this colors[0] and that will return the array for the color black. So if colors[0] returns an array, than I can call from it once again, like this colors[0][1] and that will give me the second number of the rgb array.
I hope this helps.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Nov-18-2019, 10:15 AM
(This post was last modified: Nov-18-2019, 10:15 AM by perfringo.)
Maybe you don't understand how names work in Python?
In function Hi2 you assign to name 'a' a new value (empty matrix). It doesn't matter with which argument you call this function, it will always deal with empty 'a' assigned in body of the function.
What are you trying to accomplish?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 22
Threads: 12
Joined: Nov 2019
Nov-18-2019, 06:19 PM
(This post was last modified: Nov-18-2019, 06:20 PM by Reldaing.)
Hi, I'm trying to get familiar with array. Right now, I've made something like a sodoku: Here's my code:
def Hi1():
a = [[1, 2, 3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 1, 2, 3], [7, 8, 9, 1, 2, 3, 4, 5, 6], [2, 3, 1, 5, 6, 4, 8, 9, 7], [5, 6, 4, 8, 9, 7, 2, 3, 1],[8, 9, 7, 2, 3, 1, 5, 6, 4], [3, 1, 2, 6, 4, 5, 9, 7, 8], [6, 4, 5, 9, 7, 8, 3, 1, 2], [9, 7, 8, 3, 1, 2, 6, 4, 5]]
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j], end=' ')
print() Output: >>> Hi1()
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 1 5 6 4 8 9 7
5 6 4 8 9 7 2 3 1
8 9 7 2 3 1 5 6 4
3 1 2 6 4 5 9 7 8
6 4 5 9 7 8 3 1 2
9 7 8 3 1 2 6 4 5
The problem is that I don't know how to turn this code in a fonction which takes lists in argument(I want to enter the lists in the console, and then it gives me the sodoku). This is what I've made so far. Could you please fix my prblem? Thanks !
def Hi2(a):
a=[[],[],[],[],[],[],[],[]]
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j], end=' ')
print()
Posts: 360
Threads: 5
Joined: Jun 2019
Have a look at this sample. I advise you work through a python tutorial to learn the basics.
def enter_sudoku(size):
sudoku = []
for i in range(size):
row = [int(number) for number in list(input(f"Enter exactly {size} digits [1-9] without spaces: "))]
sudoku.append(row)
return sudoku
def show_sudoku(sudoku):
for row in sudoku:
for number in row:
print(f"{number}", end=' ')
print()
matrix = enter_sudoku(9)
show_sudoku(matrix)
|