[英]How can I detect if a dictionary has certain criteria?
I'm trying to implement a save feature into my game to save a variety of information. 我试图在我的游戏中实现保存功能,以保存各种信息。 The save part works perfectly fine, but the load part does not work. 保存部分工作得很好,但是加载部分不起作用。 This is my current dictionary: 这是我当前的字典:
player_data = {'x':x, 'y':y, 'world':mapSection, 'introcomplete':homeintro}
I am loading/saving with this: 我正在加载/保存此:
def save_game():
with open("savegame", "wb") as f:
pickle.dump(player_data, f)
def load_game():
with open("savegame", "rb") as f:
global player_data
player_data = pickle.load(f)
And to see what mapSection is set as, I use this: 要查看将mapSection设置为什么,我使用以下代码:
load_game()
print(player_data)
if "1_1" in player_data:
print('maploaded')
game_map_1_1()
else:
print('reset')
game_intro()
But for some reason, it always skips over the if statement to the else statement. 但是由于某种原因,它总是跳过if语句到else语句。 I don't know what I am doing wrong. 我不知道我在做什么错。
I'm guessing what you really want to do is check player_data['world'] == '1_1'
and not '1_1' in player_data
. 我猜您真正想做的是在player_data['world'] == '1_1'
中检查player_data['world'] == '1_1'
而不是'1_1' in player_data
。 The second one checks if you have a key named 1_1
. 第二个检查您是否有一个名为1_1
的密钥。
This is not specific to pickle. 这不是特定于泡菜。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.