list indices must be integers or slices, not str
列表索引 必须是 整数 或 切片,而不是str
aha ! eval()
space for eval() official code
inp2=input()
print("just out put the input:\n",inp2)
print("and this is type of input:",type(inp2))
b=eval(inp2)
print("what if i eval(input):\n",b)
print("so type of eval(str type) is:",type(b))
c=inp2.split(',')
print("just print c[ c=str.split() ]:\n",c)
print("and this is type of input:",type(c))
print("what if i eval(list)?")
b=eval(c)
input : 1,3,6,9,2,5,1
just out put the input:
1,3,6,9,2,5,1
and this is type of input: <class 'str'>
what if i eval(input):
(1, 3, 6, 9, 2, 5, 1)
so type of eval(str type) is: <class 'tuple'>
just print c[ c=str.split() ]:
['1', '3', '6', '9', '2', '5', '1']
and this is type of input: <class 'list'>
what if i eval(list)?
error for eval(b)
def getnum():
inp=input() #get input
print("inp is :\n",inp)
print("type of inp:",type(inp))
print("the item type of inp[0]: ",type(inp[0]))
input1=inp.split(',')
print("input1 is:\n",input1)
print("type of input1:",type(input1))
print("the item type of input1[0]: ",type(input1[0]))
test=eval(inp)
print("eval(inp<--str) is :\n",test)
print("type of test is :",type(test))
print("the item type of test[0]: ",type(test[0]))
print("the item type of eval(int): ",type(eval(test[0])))
input2=list(eval(inp))
print("input2 is : \n",input2)
print("the item type of input2[0]: ",type(input2[0]))
return input2
getnum()
input : 1,3,6,9,2,5,1
inp is :
1,3,6,9,2,5,1
type of inp: <class 'str'>
the item type of inp[0]: <class 'str'>
input1 is:
['1', '3', '6', '9', '2', '5', '1']
type of input1: <class 'list'>
the item type of input1[0]: <class 'str'>
eval(inp<--str) is :#output of test
(1, 3, 6, 9, 2, 5, 1)
type of test is : <class 'tuple'>
the item type of test[0]: <class 'int'>
input2 is :
[1, 3, 6, 9, 2, 5, 1]
the item type of input2[0]: <class 'int'>
what if i eval(int)?
error for eval(test[0])
error for eval(test[0]) && error for eval(b)
b=eval(c)
TypeError: eval() arg 1 must be a string, bytes or code object
eval() arg1必须是一个字符串、字节或代码对象