Python 无法将数据插入列表-AttributeError:';int';对象没有属性';插入';
由于无法在列表中插入值,因此当前处于停滞状态 尝试输入列表“sex”时,我返回以下错误: 'AttributeError:'int'对象没有属性'insert'' 到目前为止,我得到的是:Python 无法将数据插入列表-AttributeError:';int';对象没有属性';插入';,python,list,insert,Python,List,Insert,由于无法在列表中插入值,因此当前处于停滞状态 尝试输入列表“sex”时,我返回以下错误: 'AttributeError:'int'对象没有属性'insert'' 到目前为止,我得到的是: #input/error handling error = 'Error, Incorrect (format/value)' returned = 'Returned.' entryRem = 'Entry removed.' na = 'N/A' #data storage index = [0] se
#input/error handling
error = 'Error, Incorrect (format/value)'
returned = 'Returned.'
entryRem = 'Entry removed.'
na = 'N/A'
#data storage
index = [0]
sex = [0]
choice = int()
def menu():
print('1. Input data')
input1 = input('Input (1): ')
if input1 == '1':
indexSel(index)
sexInput(sex, choice)
else:
print('\n',error,returned,'\n')
menu()
return
def indexSel(index):
global choice
print('Index: ',index)
choice = len(index)
index.append(choice)
return
def sexInput(choice, sex):
inSex = input("Person's Sex? (m/f)").upper()
if inSex == 'M' or inSex == 'F':
sex.insert(choice,inSex)
else:
print(entryRem,error)
return
menu()
在main中,通过以下部分调用sexInput:
if input1 == '1':
indexSel(index)
sexInput(sex, choice)
然后,函数的标题为:
def sexInput(choice, sex):
因此,您切换了顺序,从而在您认为应该是sex(一个列表)的变量中做出选择(一个int)变量和函数名应该遵循带有下划线的
小写形式<代码>返回本身也是不好的样式。总的来说,肯定有一些重构要做。谢谢,伙计,我没有意识到这会产生错误,我会在将来记住的