在做练习时,遇到一个小的出错信息,虽然结果出来了,但还是有错误提示,有点迷惑,高手路过给予指点。
题目是这样的,要求从用户那里接受一个字符串输入,然后逐字符显示该字符串。用while循环实现。
我编的程序如下:
>>> user=raw_input('enter a string : ')
enter a string : abcdefg
>>> n=0
>>> while n<=len(user):
print user[n]
n+=1
a
b
c
d
e
f
g
Traceback (most recent call last):
File "<pyshell#72>", line 2, in <module>
print user[n]
IndexError: string index out of range
>>>
虽然题目的要求达到了,但是有点不明白为什么会出现下面的错误提示,不知道应该怎么改写才能避免出错信息。特此记下,待以后解决。