Python-将变量设置为int然后永久更改

时间:2019-07-09 15:02:57

标签: python python-3.x

我正在为妈妈的工作开发程序,并尝试设置一个变量col = 6,然后在完成循环后以col = 7的方式添加到变量col = col + 1中,保持这种方式。但是,我无法坚持下去,它总是恢复为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)

我希望col在运行后从6更改为7,然后再更改为8,9,10等,并在我关闭并一遍又一遍地运行文件时使其保持不变。

2 个答案:

答案 0 :(得分:0)

我认为您的问题出在此部分:

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

您每次都会重新定义col = 6。尝试移动变量定义的位置

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

答案 1 :(得分:0)

我在这里找到了答案,我改变了方法,决定找到寻找空单元格的代码

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)