无法将数据插入列表AttributeError:“int”对象没有属性“insert”

2024-09-30 16:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

目前由于无法在列表中插入值而陷入困境

在尝试输入“sex”列表时,返回以下错误:

'AttributeError:'int'对象没有'insert'属性

到目前为止我得到的是:

#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()

Tags: 列表inputindexreturndeferrorintmenu
1条回答
网友
1楼 · 发布于 2024-09-30 16:22:07

在main中,通过以下部分调用sexInput:

if input1 == '1':
    indexSel(index) 
    sexInput(sex, choice)

然后,函数的标题为:

def sexInput(choice, sex):

所以,你改变了顺序,从而在你认为是性的东西(一个列表)中做出了选择(一个int)

相关问题 更多 >