-2

I am working on a program for my mum's work and trying to set a variable such as col = 6 then after finishing the loop adding to it to make col = 7 in the way col = col + 1 and keeping it that way. However, I cannot make it stick and it always reverts back to being col = 6.

first  = input("What would you like to do; view, add or edit? ").lower()

if first == 'view':
    print(allcells)

if first == 'add':
    add = input("What would you like to add; pupil or development? ").lower()
if add == 'pupil':
    col = 6
    surname = input("Please input surname ").lower()
    sheet.update_cell(col, 1, surname)
    col = col + 1
    print (col)

I would like col to change from 6 to 7 after running it, then furthermore to 8,9,10 etc. and for it to stay as I close and run the file over and over.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    well, you're always resetting it to 6 when you want to add a pupil. try declaring it somehere else (suitable for your needs, before the if) and it should work – rebecca Jul 09 '19 at 15:10
  • i have tried multiple locations and all seem to reset back to 6, any ideas on alternative code rather than placement? – charlie fish Jul 09 '19 at 15:12
  • then i'd assume you set it in the scope that gets "entered" anew everytime. normally a variable doesn't just reset itself. hard to tell with the provided code snippet – rebecca Jul 09 '19 at 15:14
  • the code its self isnt very long at the minute, the way im looking at it is, every time the program runs, "col = 6" is also ran, resetting the variable to 6, is there any way around it in a way that col = '' rather than 6 so it doesnt get set to 6 every time the program is ran – charlie fish Jul 09 '19 at 15:17
  • why is it even 6, is this the first empty cell or is there other stuff before? (you shouldn't hardcode that btw) – rebecca Jul 09 '19 at 15:22
  • the program that i am trying to create is, they can input what they wish to add/edit, in the case the pupil, then when chosen, it goes into further detail, https://gyazo.com/bd29105874a4417092bfc02eed100329 , that table is what they will be viewing/editing and as it is a google sheets the first empty cell for that "surname" variable is column 6 so its "col = 6" then as it is filled i would like to add one so it moves to the next cell as im unsure on a line of code that looks for the next empty cell using google sheets and python? – charlie fish Jul 09 '19 at 15:26
  • mmh. can you show more of your code? or is this all you have? another thing i noticed (it's been a while with python for me), but shouldn't the check for pupil be indented further in to be inside the 'add' check? – rebecca Jul 09 '19 at 15:30
  • aaah wait. do you want to store the last edited col permanently? like open program, do stuff, close program... open program again? – rebecca Jul 09 '19 at 15:31
  • https://gyazo.com/96188f5279bbe9248a41d34130b4256d , there is the full lenth code and answer, as i said, i have only just started this program and it is no where near the end, i would like to get this program done but not sure if it is a bit out of my league haha! – charlie fish Jul 09 '19 at 15:31
  • Yess! thats exactly what i would like to do, in order for it to stay with the open cell in the sheets document – charlie fish Jul 09 '19 at 15:32
  • ah, sorry i totally misunderstood you. well you need to store the value somewhere outside the program and load it back in when you run the program where you then update it again and overwrite the previous value. or, if possible, you could check which col is "empty" and start from there. but right now, you're always using the same variable and value on restart – rebecca Jul 09 '19 at 15:35
  • or let the user decide in which col to start and use that. – rebecca Jul 09 '19 at 15:36
  • Possible duplicate of [Saving and recovering values of variables between executions](https://stackoverflow.com/questions/13887798/saving-and-recovering-values-of-variables-between-executions) – Georgy Jul 09 '19 at 15:40
  • how would this be done, using a text file i know, however im unsure on how to open the text file and use that integer as a position in the sheet.update_cell() line of code? – charlie fish Jul 09 '19 at 15:44

2 Answers2

0

I think your problem is coming from this chunk:

if add == 'pupil':
    col = 6
    surname = input("Please input surname ").lower()
    sheet.update_cell(col, 1, surname)
    col = col + 1
    print (col)

You are redefining col = 6 every single time. Try moving the placement of the defining of the variable

col = 6
if add == 'pupil':
    surname = input("Please input surname ").lower()
    sheet.update_cell(col, 1, surname)
    col = col + 1
    print (col)
FiercestJim
  • 198
  • 1
  • 7
  • Thanks for your reply, however once again, after re-running the program it resets the variable back to 6? https://gyazo.com/96188f5279bbe9248a41d34130b4256d – charlie fish Jul 09 '19 at 15:29
  • OK I now understand your problem. You the program to run set a variable close the script. Run the script again and it uses the new variable? Why do you want to do it like this? I strongly advise you try and make use of "for" loops or a "while" condition. – FiercestJim Jul 09 '19 at 15:37
  • The program that i am trying to create basically takes a premade Google Sheets file, opens it and gives you the option to view it, edit it or add a pupil. i am on the adding stage where i would like to add a pupil to the next available cell in the sheet which as far as im aware of, there isnt a line of code for, therefore i have taken the first empty cell and after adding to it would like to add 1, this would allow the next input to be in the next cell. This cell would have to save and not be reset after closing and reopening the file – charlie fish Jul 09 '19 at 15:41
  • I still don't recommend this as the way you approach this. You say there isn't a way of finding the next available column. What about something like this: ```def next_available_col(worksheet): str_list = filter(None, worksheet.row_values(1)) return str(len(str_list)+1) ``` I took this from here (modified): https://stackoverflow.com/questions/40781295/how-to-find-the-first-empty-row-of-a-google-spread-sheet-using-python-gspread – FiercestJim Jul 09 '19 at 15:49
  • This would work however it is an exact cell that i am looking for, not only a row, not only this, i will be looking for the exact cell multiple times in the same loop, for example like in the previous picture, surname, forname, class, gender. Each will be a different cell – charlie fish Jul 09 '19 at 15:53
  • But in your code you specify it's always row one. So if you use the code I provided to find the empty empty column it will work – FiercestJim Jul 09 '19 at 15:56
  • ahh, i must not have made that clear, the program will be used to edit multiple things on multiple different occasions therefore i was using 1 to test to see if i could get the column working before going onto the row. Sorry for any inconvenience – charlie fish Jul 09 '19 at 16:00
0

I found my answer here, i changed my approach and decided to find code which looked for the empty cells

def find_empty_cell():
alphabet = list(map(chr, range(65, 91)))
for letter in alphabet[0:1]:
    for x in range(1, 1000):
        cell_coord = letter+ str(x)
        if sheet.acell(cell_coord).value == "":
            print(cell_coord)