-5

This is my code. In the first def function, I made it return column_choose, and I wanna use column_choose's value in second def function(get_data_list). What can I do? I have tried many times. But IDLE always show:global name 'column_choose' is not defined.

How to use column_choose's value in second function? By the way, I use python 3.2

def get_column_number():
while True:
    column_choose = input('What column:')
    if column_choose == '1' or column_choose == '2' or column_choose == '3' or column_choose == '4' or column_choose == '5' or column_choose == '6':
        column_choose = int(column_choose)
        return column_choose
        break
    else:
        print('bad column number, try again')

def get_data_list(column_number):
new_column_number = column_number.split(',')
date_column = new_column_number[0]
choose_column = new_column_number[column_choose-1]

return result

def main():
#Call get_input_descriptor
get_input_descriptor()
#Call get_column_number
get_column_number()

file_obj = open("table.csv", "r")
for column_number in file_obj:
    column_number = (column_number.strip())
    result = get_data_list(column_number)
    print(result)

file_obj.close()
main()
4

2 回答 2

0
  1. 你的缩进格式错误?

  2. return你在你的函数中有使用get_column_number,所以你不需要break在它之后使用

  3. 该函数get_column_number返回一个变量,因此您可以使用它分配给一个变量

    就像:

    var = get_column_number()
    

    然后使用 var 变量

  4. 如果你希望它是一个全局变量,你可以使用global

我觉得你需要读一本python书来掌握基本知识

于 2013-02-25T03:52:31.970 回答
0

您需要将列号分配给变量。此外,您不需要该break声明,因为您将其放在退货之后。

#Call get_column_number
column_num = get_column_number()

另外,我不确定您是否正确编写了代码。该行 for column_number in file obj:正在遍历您的 csv 文件。如果您尝试访问特定列,您可以这样做:

content = ''
file_obj = open('table.csv','r')
content = fle_obj.split(',')
print(content[column_number])
于 2013-02-25T02:15:46.897 回答