Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help
#1
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()
Reply
#2
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()
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

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