python - Save File Function -


so intro programming class have create game save/load function , i'm trying test out code make sure works.

for reason cannot following function work properly. i've tried going through line line in idle , works fine there once try use same system in function not work. please?

def save(name,inventory,mapgrid,x,y,enemy):`      choice = 0      file = shelve.open("save_files")            save = {'save1':file['save1'],'save2':file['save2'],'save3':file['save3']}      print("where save?")     print("save 1 -", save['save1']['name'])     print("save 2 -", save['save2']['name'])     print("save 3 -", save['save3']['name'])     choice = input("enter number:\t")      if choice == 1:         save['save1']['name'] = name         save['save1']['inventory'] = inventory         save['save1']['mapgrid'] = mapgrid         save['save1']['x'] = x         save['save1']['y'] = y         save['save1']['enemy'] = enemy         file['save1'] = save['save1']         file.sync()     if choice == 2:         save['save2']['name'] = name         save['save2']['inventory'] = inventory         save['save2']['mapgrid'] = mapgrid         save['save2']['x'] = x         save['save2']['y'] = y         save['save2']['enemy'] = enemy         file['save2'] = save['save2']         file.sync()     if choice == 3:         save['save3']['name'] = name         save['save3']['inventory'] = inventory         save['save3']['mapgrid'] = mapgrid         save['save3']['x'] = x         save['save3']['y'] = y         save['save3']['enemy'] = enemy         file['save3'] = save['save3']         file.sync()      file.close()      print("game saved") 

edit: after running function should save dictionary file['save#'] , allow me access data later on, data doesn't save shelve file , when try access again there's nothing there. ((sorry should have put in right off bat))

for example if run save() function again should display name associated save file, shows 'empty'.

the basic thing have save_files set is

file['save#'] = {'name':'empty'}

since if statements comparing int, make sure choice integer. it's possible choice string, in case none of comparisons true. basically:

choice = int(input("enter number:\t")) 

alternatively change comparisons strings, important thing assure type consistency in comparisons


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -