【发布时间】:2014-05-09 18:55:45
【问题描述】:
我有以下一些 python 代码:
# we are going to define a list:
charList = []
#which we can use to add character's data to
i = 0
print("Please input data for your first character below: ")
while i <= 5:
charList[i] = input("What is their name? ")
i += 1 # increments i by 1
charList[i] = input("\nAnd what is their strength value? ")
i += 1
charList[i] = input("\nAnd what is their skill value? ")
i += 1
print("\nThank You :)\n")
print("Now for your second character:")
print(charList[0],charList[3])
这是针对 3.4.0 版的。
出现以下错误:
>>>
Please input data for your first character below:
What is their name? Chewbacca
Traceback (most recent call last):
File "C:/Users/Peter/Documents/ARCHIVES/NEW!/Computing/task3version1.0.py", line 20, in <module>
charList[i] = input("What is their name? ")
IndexError: list assignment index out of range
>>>
我猜这与在 while 循环中更改 i 的值有关。关于什么是错的任何想法?谢谢你:)
【问题讨论】:
标签: python list variables while-loop increment