Discuss / Python / 求救。。为什么打出来了还会报错

求救。。为什么打出来了还会报错

Topic source

>>> L = ['Bart', 'Lisa', 'Adam']

>>> n=-1

>>> while n<3:

n=n+1

print('Hello,'+L[n])

Hello,Bart

Hello,Lisa

Hello,Adam

Traceback (most recent call last):

  File "<pyshell#75>", line 3, in <module>

    print('Hello,'+L[n])

IndexError: list index out of range

LetHG

#2 Created at ... [Delete] [Delete and Lock User]

L 有效索引:0.1.2

while n < 3 

n = 2 + 1  => 3

啊感谢感谢!发现错误了,改成n<2终于好了

韩总裁啊

#4 Created at ... [Delete] [Delete and Lock User]

没有这么复杂吧,你看我写这个

L = ['Bart', 'Lisa', 'Adam']

for a in L:

    a = "HELLO" + "," + a + "!"

    print(a)


  • 1

Reply