Python嵌套列表-更改和替换单个项
我正在完成一个简单的编程练习(我还是新手),通过为4个不同的角色属性分配30个点来创建角色配置文件。程序功能包括:显示当前配置文件、创建新配置文件或更改现有配置文件。第一个和第二个特性工作正常,但最后一个特性有问题:程序要解包嵌套的列表项(属性+分配的分数),请求一个新分数,计算新旧之间的差异,并相应地更改池中可用的分数。最后,在位置0的列表中添加一个新条目(属性+新分配的分数),然后删除位置1的条目,该条目应该是该属性的旧条目。循环浏览列表,然后完成。但是,一旦执行代码,就会发现它无法工作。请参阅以下完整代码:Python嵌套列表-更改和替换单个项,python,list,nested,Python,List,Nested,我正在完成一个简单的编程练习(我还是新手),通过为4个不同的角色属性分配30个点来创建角色配置文件。程序功能包括:显示当前配置文件、创建新配置文件或更改现有配置文件。第一个和第二个特性工作正常,但最后一个特性有问题:程序要解包嵌套的列表项(属性+分配的分数),请求一个新分数,计算新旧之间的差异,并相应地更改池中可用的分数。最后,在位置0的列表中添加一个新条目(属性+新分配的分数),然后删除位置1的条目,该条目应该是该属性的旧条目。循环浏览列表,然后完成。但是,一旦执行代码,就会发现它无法工作。请
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.")
options=[“力量”、“健康”、“智慧”、“灵巧”]
配置文件=[]
点数=30
选择=无
而选择!="0":
印刷品(
"""
角色创建程序
0-退出
1-参见当前配置文件
2-建立新的配置文件
3-修订现有资料
"""
)
选择=输入(“请选择一个选项:”)
打印()
如果选项==“0”:
打印(“再见”)
elif选项==“1”:
对于配置文件中的项目:
打印(项目)
输入(“\n按enter键继续。”)
elif选项==“2”:
打印(“您现在可以为您的角色装备探险属性。”)
打印(“您有”,点数,“要花费的点数”)
打印(“现在配置您的角色:\n”)
#运行点分配循环
对于选项中的项目:
点=整数(输入(“请输入“+str(项目)+”:”的点)
如果点0:
点数+=差值
打印(“您的分数分配已更改为“,-diff,,-points.”)
打印(区别,“点数刚刚添加到池中。”)
打印(“池现在包含“、点”、“点”)
条目=项目,点\新
配置文件。插入(0,条目)
del配置文件[1]
输入(“按enter键继续。\n”)
elif diff=0:
点数+=差值
打印(“您的分数分配已更改为“,-diff,,-points.”)
打印(-diff,“点数刚刚从池中获取。”)
打印(“池现在包含“、点”、“点”)
条目=项目,点\新
配置文件。插入(0,条目)
del配置文件[1]
输入(“按enter键继续。\n”)
elif diff正如对你问题的评论所表明的,你没有以最好的方式提出你的问题。但是,我知道它有什么问题。我可以向您展示如何修复当前代码,但事实是,修复它的最佳方法是完全重写它。在这样做时,您应该采取以下策略:
将代码分解为多个部分。在这种情况下,我建议您创建几个不同的函数。其中一个可以称为main\u loop
,并包含在菜单中循环的逻辑。它不包含任何更新或显示配置文件的代码。相反,它将调用其他函数,display\u profile
,build\u profile
,以及amend\u profile
。这些函数将接受变量,如选项
、配置文件
和点
,并返回值,如选项
和点
。这将极大地简化您的代码,并使其更易于测试和调试。下面是一个main\u loop
的示例:
def main_loop():
options = ["Strength", "Health", "Wisdom", "Dexterity"]
profile = []
points = 30
choice = None
while choice != "0":
print(menu_string) #define menu_string elsewhere
choice = input("Please choose an option: ")
print()
if choice == "0":
print("Good bye.")
elif choice == "1":
display_profile(profile)
elif choice == "2":
profile, points = build_profile(options, profile, points)
elif choice == "3":
profile, points = amend_profile(profile, points)
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.")
看看这有多好?现在您所要做的就是定义其他函数。类似于
def build_profile(options, profile, points):
# insert logic here
return profile, points
这种方法的另一个优点是,现在您可以单独测试这些函数,而无需运行整个程序
使用正确的习惯用法修改列表。在迭代时修改列表需要特别小心,在某些情况下(例如,当您通过删除或添加已迭代的项来更改列表的长度时),它根本不起作用。有很多方法可以实现您试图对概要文件所做的操作,但是对于一个刚开始的程序员,我推荐一些更简单的方法:创建一个新的列表!然后返回该列表。因此,在您的amend_profile
函数中,执行以下操作:
def amend_profile(profile, points):
# other code ...
new_profile = []
for item in profile:
attribute, points_aloc = item
# other code ...
new_proflie.append(entry)
# other code ...
return new_profile, points
还要注意的是,这是您的一个主要错误所在;您创建了一个包含(item,point\u new)
而不是(attribute,point\u new)
的条目
,因此新元组中有一个item
元组,而不是预期的单个属性
字符串
欢迎来到堆栈溢出。你能澄清你的问题吗?它应该包含一个;对问题的明确描述(包括错误消息!)以及尝试修复问题时的描述。此外,还应包含预期的输入和输出。非常感谢您的帮助!!并感谢所有其他评论;这是我在这里的第一篇文章。将在将来采纳建议:-)