在我的代码中,我试图让用户输入以选择数组中的一个值,但是当有一个固定值(例如 2)时它可以工作,但是当提到 input() 时它就不起作用。
highstreet = ['tp' ,'io','fffffff','mmmmmm','ice']
x = input
highstreet[x]
print(highstreet[x])
>> highstreet[x]
>>TypeError: list indices must be integers or slices, not
builtin_function_or_method
谢谢
请您参考如下方法:
问题是 input 是一个 function 而不是 variable,因此你必须先调用它来分配输入的值到变量 x。更重要的是,错误非常具体 TypeError: list indices must be integers or slice, not builtin_function_or_method,这意味着变量 x 是一个函数,这是因为你赋值了在这种情况下,它是一个函数 input。
x = int(input()) # int(raw_input()) in python 2
highstreet[x]
print(highstreet[x])
