我正在完成一个简单的编程练习(我还是新的),其中我通过为4个不同的字符属性分配30个点来创建角色配置文件。计划功能包括:显示当前配置文件,创建新配置文件或更改现有配置文件。第一个和第二个功能工作正常,但最后一个问题:该程序是为了解开嵌套列表项目(属性+分配的分数),要求新的分数,采取旧的和新的差异,并改变数字相应的池中可用点数。最后,在位置0处向列表添加一个新条目(属性+新分配的分数),然后删除位置1处的条目,该条目应该是该属性的旧条目。遍历列表并完成。但是,一旦执行代码,您将看到它不起作用。请参阅下面的完整代码:Python嵌套列表 - 更改和替换单个项目
options = ["Strength", "Health", "Wisdom", "Dexterity"]
profile = []
points = 30
choice = None
while choice != "0":
print(
"""
CHARACTER CREATOR PROGRAM
0 - Exit
1 - See current profile
2 - Build new profile
3 - Amend existing profile
"""
)
choice = input("Please choose an option: ")
print()
if choice == "0":
print("Good bye.")
elif choice == "1":
for item in profile:
print(item)
input("\nPress the enter key to continue.")
elif choice == "2":
print("You can now equip your character with attributes for your adventures.")
print("You have",points,"points to spent.")
print("Now configure your character: \n")
#Run the point allocation loop
for item in options:
point_aloc = int(input("Please enter points for " + str(item) + ":"))
if point_aloc <= points:
entry = item, point_aloc
profile.append(entry)
points = points - point_aloc
print("\nYour current choice looks like this: ")
print(profile)
input("\nPress the enter key to continue.")
else:
print("Sorry, you can only allocate", points," more points!")
print("\nYour current choice looks like this: ")
print(profile)
input("\nPress the enter key to continue.")
print("\nWell done, you have configured your character as follows: ")
for item in profile:
print(item)
input("Press the enter key to continue.")
elif choice == "3":
print("This is your current character profile:\n")
for item in profile:
print(item)
print("\nYou can change the point allocation for each attribute.")
for item in profile:
point_new = int(input("Please enter new points for " + str(item) + ":"))
attribute, points_aloc = item
diff = points_aloc - point_new
if diff >0:
points += diff
print("Your point allocation has changed by", -diff,"points.")
print(diff,"points have just been added to the pool.")
print("The pool now contains", points,"points.")
entry = item, point_new
profile.insert(0, entry)
del profile[1]
input("Press the enter key to continue.\n")
elif diff <0 and points - diff >=0:
points += diff
print("Your point allocation has changed by", -diff,"points.")
print(-diff,"points have just been taken from the pool.")
print("The pool now contains", points,"points.")
entry = item, point_new
profile.insert(0, entry)
del profile[1]
input("Press the enter key to continue.\n")
elif diff <0 and points - diff <=0:
print("Sorry, but you don't have enough points in the pool!")
input("Press the enter key to continue.\n")
else:
print("Sorry, but this is not a valid choice!")
input("Press the enter key to continue.\n")
input("\n\nPress the enter key to exit.")
注意:您需要先创建配置文件以运行更改。
在此先感谢您的帮助!
欢迎来到Stack Overflow。你能否澄清你的问题?它应该包含一个[简短,独立,正确的例子](http://sscce.org/);在尝试时明确描述问题所在(包括错误消息!)和[您尝试过的内容](http://mattgemmell.com/2008/12/08/what-have-you-tried/)的描述解决问题。 – Ben 2012-04-21 15:13:14
另外,它应该包含预期的输入和输出。 – 2012-04-21 15:18:26