Python Forum
Help - 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 (/thread-2477.html)



Help - Codershadow - Mar-20-2017

I've written this to get a grid in python and I want to know how to change the numbers in the grid to go up like 1,2,3,4 etc... instead of all being 0
def gridSize_def():
    global gridSize
    while True:
        try:
            gridSize = int(input("input grid length and width\n"))
            if gridSize<2:
                print ("invalid int")
            else:
                break
        except ValueError:
            print ("invalid input") 
    grids = [[] for _ in range (gridSize)]
    for i in range(0,gridSize):
        grids[i-1]=[0 for _ in range(gridSize)]
    return grids
def grid_def():
    for i in range (0,gridSize):
        print (grids[i-1])
grids = gridSize_def()
grid_def()



RE: Help - kapoor1992 - Mar-20-2017

I am new to python but i guess the below code should help
def gridSize_def():
   global gridSize
   while True:
       try:
           gridSize = int(input("input grid length and width\n"))
           if gridSize<2:
               print ("invalid int")
           else:
               break
       except ValueError:
           print ("invalid input")
   grids = [[] for _ in range (gridSize)]
   j = 0 #Changes were made here
   j = j + 1 #Changes were made here
   for i in range(0,gridSize):
       grids[i-1]=[j for j in range(gridSize)] #Changes were made here
   return grids
def grid_def():
   for i in range (0,gridSize):
       print (grids[i-1])
grids = gridSize_def()
grid_def()



RE: Help - nilamo - Mar-20-2017

Quote:grids[i-1]=[0 for _ in range(gridSize)]

If you don't want them all set to 0, stop setting them all to 0 :p