所以,当我只是涉猎编码和实验的时候,我想为我的D&;D运动需要
事实证明,在不理解代码的情况下将代码拼凑在一起并不总是有效的,令人震惊
所以,这是我的代码,虽然我确信它会让你的眼睛流血,但我只需要让它工作:
#import pickle
import pickle
#NPC ID generator
counter=1
NPCS=[]
while counter<=170 :
NPCS.append(counter)
counter+=1
if len(NPCS)==170:
print ("True")
else :
print ("False") ; raise SystemExit
#Attributes
name=[] ; occupation=[];weakness=[];need=[];desire=[];enemy=[];rumor=[];secret=[];passion=[]
redeemdamningquality=[];happy=[];occdesire=[];occcomplication=[];pcopinion=[];accomplish=[]
magical=[];politinfl=[];resource=[];intel=[];research=[]
NPCatt=[name,occupation,weakness,need,desire,enemy,rumor,secret,passion,redeemdamningquality,happy,occdesire,occcomplication,pcopinion, accomplish,magical,politinfl,resource,intel,research]
#open a pickle file
newfile = 'NPCatt.pk'
#load your data back to memory when you need it
with open(newfile, 'rb') as fi:
NPCatt = pickle.load(fi)
# Data Input
print ("Enter the numerical code of the NPC you wish to modify")
raw=int(input())
if raw != ValueError :
print ("Enter Name of NPC" + str(raw) ) ; a=input()
if a!="":
name.insert(raw+1,a);print ("Name Inserted Successfully")
else:
print ("Skipped!")
print ("Enter Occupation of NPC" + str(raw) ) ;a=input()
if a!="":
occupation.insert(raw+1,a);print("Occupation Inserted Successfully")
else:
print ("Skipped!")
else :
print ("BAD VALUE")
for x in (NPCatt) :
if len(x)!=0 :
print (x)
elif len(x)>=170:
print (x) ; print ("Has too many items")
else :
print (str(x) + "is empty")
with open(newfile, 'wb') as fi:
# dump your data into the file
pickle.dump(NPCatt, fi)
我不确定的是为什么我输入的数据在代码运行之间没有“保存”。 请帮忙
我知道这根本不是你想要的,但是你使用电子表格不是更容易吗
您正在手动输入数据并将其填充到二维数据结构中。
如果需要在其他地方使用数据,请将电子表格另存为.csv文件并导入该文件
你的问题是你对变量和任务如何工作的理解。在下面的代码中,您创建了许多列表。然后创建一个名为NPCatt的变量,该变量引用您创建的所有这些列表
因此,如果我看NPCatt[0],它将是一个所有npc名称的列表。这很好。然而,你继续做下去
现在变量NPCatt并不指向所有列表。它现在指向未勾选的对象。因此,当您稍后执行names.append时,它将更新名称列表,但NPCatt不再指向此列表。因此,当你腌制NPCatt时,只需腌制你从文件中加载的内容
这是你错误的症结所在。如果您想在取消勾选后修改NPCatt保存的数据,那么您应该像这样访问它
然而,这变得非常混乱,不清楚哪个列表正在更新,因为您必须按索引位置引用它。您最好在这里查看python字典,因为您可以通过名称而不是索引位置来引用内容。或者,如果你准备创建一个NPC类,然后通过NPC\u ID:NPC\u class\u实例将每个NPC存储在dict中,那就更好了
更新
下面是一个简单的例子,我用一个npc类和一个dict按id存储npc。这只是一个简单的例子,没有任何关于设计或pro和con的真实想法,只是为了给大家展示一个例子
相关问题 更多 >
编程相关推荐