又要麻烦各位大神了
def sum1(*a,base = 3):result = 0
list1 = []
for b in a:
list1 = list1.append(b)
if list1[-1] == 5:
base == 5
result+=b
result = result * base
print(result)
sum1(12,2,3,5)
然后报错了:
Traceback (most recent call last):
File "E:/Python/小甲鱼课后练习/第18讲/sum.py", line 12, in <module>
sum1(12,2,3,5)
File "E:/Python/小甲鱼课后练习/第18讲/sum.py", line 6, in sum1
if list1[-1] == 5:
TypeError: 'NoneType' object is not subscriptable
这个要怎么改啊 本帖最后由 wp231957 于 2021-3-23 16:26 编辑
def sum1(*a,base = 3):
result = 0
list1 = []
for b in a:
list1.append(b)
if list1[-1] == 5:
base = 5
result+=b
result = result * base
print(result)
sum1(12,2,3,5) def sum1(*a,base = 3):
return sum(a) * base
print(sum1(12,2,3,5))
print(sum1(12,2,3,5,base=5))
def sum1(*a,base = 3):
if list(a)[-1]==5:
base=5
return sum(a) * base
print(sum1(12,2,3,5)) wp231957 发表于 2021-3-23 16:12
def sum1(*a,base = 3):
result = 0
list1 = []
为什么list1 == list1.append(b) 改成了 list1.append(b)就好了{:5_99:} 爱吃肥肉的马 发表于 2021-3-23 16:40
为什么list1 == list1.append(b) 改成了 list1.append(b)就好了
因为列表的append方法并没有返回值
所以你给他赋值,那么就会变成none 逃兵 发表于 2021-3-23 16:24
这个厉害,学习了{:5_106:}
页:
[1]