Python_something/just for fun

12 篇文章 0 订阅
9 篇文章 0 订阅

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必须是一个字符串、字节或代码对象


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值