【问题标题】:Python - Select string from a list based on index number - index number entered via inputPython - 根据索引号从列表中选择字符串 - 通过输入输入的索引号
【发布时间】:2021-12-12 19:15:16
【问题描述】:

我正在尝试帮助某人从列表中提取设施列表,而无需输入设施的全名。相反,我认为将一个数字与每个设施相关联并允许用户输入相应的数字来获取字符串会更容易。例如,如果我有一个“设施 a”、“设施 b”和“设施 c”的列表,如果用户输入“0”,“设施 a”会出现,1“设施 b”会出现,等等。我知道我的代码不正确,但我正在努力根据用户通过输入命令输入的数字来完成这项工作。请参阅下面的代码。

facilities = ['facility a', 'facility b', 'facility c']

for count, value in enumerate(facilities):
    j = (count, value)
    print(j) #This is just so the user can see all possible options in the list 

f = input('Please enter the corresponding number of the facility you want to select: ')

final_facility = facilities[f] # I know this doesn't work and it wants a number, not a string

print(final_facility)

【问题讨论】:

  • f = int(input(...))?
  • facilities[int(f)]?用 try catch 包装它并显示一些不错的错误消息

标签: python string list indexing enumeration


【解决方案1】:

使用int 将输入转换为整数,如下所示:

f = int(input('Please enter the corresponding number of the facility you want to select: '))

确切地说,无论何时您想访问索引处的元素,都可以使用int(f),而不是直接传递f

【讨论】:

    【解决方案2】:

    默认情况下,input() 返回一个字符串。但是,列表索引不能是字符串。所以,它需要是这样的

    f = int(input('Please enter the corresponding number of the facility you want to select: '))
    

    【讨论】:

      【解决方案3】:

      或者,您可以自动将输入类型转换为: f = int(input('Please ...'))

      【讨论】:

        【解决方案4】:
        facilities = ['facility a', 'facility b', 'facility c']
        
        for count, value in enumerate(facilities):
            j = (count, value)
            print(j) #This is just so the user can see all possible options in the list
        
        f = input('Please enter the corresponding number of the facility you want to select: ')
        
        final_facility = facilities[int(f)] # Pass int(f) instead of f
        
        print(final_facility)
        

        facilities = ['facility a', 'facility b', 'facility c']
        
        for count, value in enumerate(facilities):
            j = (count, value)
            print(j) #This is just so the user can see all possible options in the list
        
        f = int(input('Please enter the corresponding number of the facility you want to select: ')) # Typecast your input to int
        
        final_facility = facilities[f] 
        
        print(final_facility)
        

        确保处理 ValueErrors 的异常。

        【讨论】:

          【解决方案5】:

          就在最后的设施之前,写f=int(f) 那应该解决它。因为,用户输入在 python 中默认是一个字符串,所以现在,我们将 f 类型转换为一个整数来解决它。 还可以尝试使用字典,这将完全消除这个麻烦。 为了得到答案,在 for 循环之后你可以直接写: print(facilities (int(f))).

          【讨论】:

            【解决方案6】:

            您需要检查输入是否为数字,然后将其转换为 int。像这样:

            facilities = ['facility a', 'facility b', 'facility c']
            
            for count, value in enumerate(facilities):
                j = (count, value)
                print(j) #This is just so the user can see all possible options in the list 
            
            f = input('Please enter the corresponding number of the facility you want to select: ')
            
            final_facility = 'Please eneter a number' # Prints this if f is not numeric
            if f.isnumeric():
                final_facility = facilities[int(f)] # I know this doesn't work and it wants a number, not a string
            
            print(final_facility)
            

            【讨论】:

              猜你喜欢
              • 2017-10-17
              • 1970-01-01
              • 1970-01-01
              • 2021-01-28
              • 1970-01-01
              • 1970-01-01
              • 2011-04-03
              • 1970-01-01
              • 2014-07-06
              相关资源
              最近更新 更多