已采纳回答 / 欧阳文轩
你是想求1000内偶数和吧,首先while循环条件错了,应该是b<1000;其次循环体也有问题,break在这里没有必要;还有print函数的缩进有问题。正确代码如下:一、不需要breaka = 0b = 0while(a <= 1000) : b += a a += 2print(b)二、需要breaka = 0b = 0while True : if a > 1000 : break b += a a += 2print(b)
2021-06-27
# Enter a code
L = [95.5, 85, 59, 66, 72]
sub_L = L[0:3]
print(sub_L)
L = [95.5, 85, 59, 66, 72]
sub_L = L[0:3]
print(sub_L)
2021-06-24
a=0
L = ['Alice', 66, 'Bob', True, 'False', 100]
for item in L:
a=a+1
if a % 2 == 0:
print(item)
L = ['Alice', 66, 'Bob', True, 'False', 100]
for item in L:
a=a+1
if a % 2 == 0:
print(item)
2021-06-24
>>> names=['Alice','Bob','Candy','David','Ellena']
>>> names.append('Zero')
>>> names.insert(5, 'Gen')
>>> names.insert(6, 'Phoebe')
>>> print(names)
['Alice', 'Bob', 'Candy', 'David', 'Ellena', 'Gen', 'Phoebe', 'Zero']
应该是上面这样吧,按照参考答案的话 'Gen', 'Phoebe'会反过来
>>> names.append('Zero')
>>> names.insert(5, 'Gen')
>>> names.insert(6, 'Phoebe')
>>> print(names)
['Alice', 'Bob', 'Candy', 'David', 'Ellena', 'Gen', 'Phoebe', 'Zero']
应该是上面这样吧,按照参考答案的话 'Gen', 'Phoebe'会反过来
2021-06-24
L = [95.5,85,59,66,72]
>>> print(L[-5],L[-4],L[-1])
95.5 85 72
>>> print(L[-5],L[-4],L[-1])
95.5 85 72
2021-06-24
>>> L = [95.5,85,59,66,72]
>>> print(L[0,1,4])
Traceback (most recent call last):
File "<pyshell#102>", line 1, in <module>
print(L[0,1,4])
TypeError: list indices must be integers or slices, not tuple
>>> print(L[0],L[1],L[4])
95.5 85 72
>>> print(L[0,1,4])
Traceback (most recent call last):
File "<pyshell#102>", line 1, in <module>
print(L[0,1,4])
TypeError: list indices must be integers or slices, not tuple
>>> print(L[0],L[1],L[4])
95.5 85 72
2021-06-24