用户从编号列表中输入并返回列表

时间:2019-04-29 06:44:34

标签: python list input

我有这个程序,可以在其中读取和显示列表,并用逗号分隔。我还为每个列表编号。我要求用户从该列表中选择一个数字。从他们选择的数字,我试图返回它附带的实际列表值。由于某种原因,我无法仅返回列表值。

因此,如果我选择输入“ 2”(整数)作为输入,它将返回“客户”。

到目前为止,这是我的代码:

list1 = ['ID', 'Customers', 'Employees', 'Orders']

def display(list1):
    counter = 0
    record = []
    for tables in list1:
        counter += 1
        record.append(tables)
        print("%s. %s" % (counter, tables))
    return record

def get_list(record):
    print("\nPick a number:")
    choose = input()
    choose = int(choose)

    x = []
    if choose in record:
        x.append(record)
        print(x)

record = display(list1)
get_list(record)

5 个答案:

答案 0 :(得分:2)

您可以改用dict

list1 = ['ID', 'Customers', 'Employees', 'Orders']

def display(list1):
    counter = 0
    record = {}
    for tables in list1:
        counter += 1
        record[counter] = tables
        print("%s. %s" % (counter, tables))
    return record

def get_list(record):
    print("\nPick a number:")
    choose = input()
    choose = int(choose)

    if choose in record:        
        print(record[choose])

record = display(list1)
get_list(record)

输出:

1. ID
2. Customers
3. Employees
4. Orders

Pick a number:
2
Customers

您的record变量如下所示:

{1: 'ID', 2: 'Customers', 3: 'Employees', 4: 'Orders'}

答案 1 :(得分:1)

您需要使用choose索引通过以下方式从record键中选择项目:   print(record[int(choose)-1])

因此代码将更改为

list1 = ['ID', 'Customers', 'Employees', 'Orders']

def display(list1):
    counter = 0
    record = []
    for tables in list1:
        counter += 1
        record.append(tables)
        print("%s. %s" % (counter, tables))
    return record

def get_list(record):
    print("\nPick a number:")
    choose = input()
    choice = int(choose)-1
    print(record[choice])

record = display(list1)
get_list(record)

您还可以按照以下步骤简化代码

li = ['ID', 'Customers', 'Employees', 'Orders']

def display(li):
    #Iterate through the list using enumerate and print
    for idx, tables in enumerate(li):
        print("%s. %s" % (idx+1, tables))

def get_list(li):
    choose = int(input("\nPick a number:"))-1
    #If choose is not a valid index in list, print error and return empty string
    if choose < 0 or choose > (len(li)-1):
        print('Invalid Choice')
        return ''
    #Else return chosen string
    return li[choose]

display(li)
print(get_list(li))

输出将是

1. ID
2. Customers
3. Employees
4. Orders

Pick a number:
3
Employees

1. ID
2. Customers
3. Employees
4. Orders

Pick a number:0
Invalid Choice

答案 2 :(得分:1)

您只需访问用户输入的列表索引

list1 = ['ID', 'Customers', 'Employees', 'Orders']

def display(list1):
    counter = 0
    record = []
    for tables in list1:
        counter += 1
        record.append(tables)
        print("%s. %s" % (counter, tables))
    return record

def get_list(record):
    print("\nPick a number:")
    choose = input()
    choose = int(choose)
    print(record[choose-1])

record = display(list1)
get_list(record)

答案 3 :(得分:1)

您可以通过单个功能执行此操作,请参见以下代码:

list1 = ['ID', 'Customers', 'Employees', 'Orders']



def display(list1):
    maxnum = len(list1)
    print("\nPick a number from 1 to %s:"%(maxnum+1))
    choose = input()
    choose = int(choose)
    if choose > maxnum:
        print("enter number less than %s"%(maxnum+1))
        return
    choose = choose-1

    print (list1[choose])

display(list1)

答案 4 :(得分:1)

在Python中,列表索引从0开始,要获得所需的输出,您需要将输入值减去1。

    list1 = ['ID', 'Customers', 'Employees', 'Orders']

    def display(list1):
        counter = 0
        record = []
        for tables in list1:
            counter += 1
            record.append(tables)
            print("%s. %s" % (counter, tables))
        return record

    def get_list(record):
        print("\nPick a number:")
        choose = input()
        choose = int(choose)
        print(record[choose - 1])    

    record = display(list1)
    get_list(record)

输出

  1. ID
  2. 客户
  3. 员工
  4. 订单

选择一个电话号码: 2 客户