python-3.x - How do I change my code from looking like this to be better formatted?
问题描述
Currently writing a bot in discord py Game that i have created is Hangman
I recently had a problem of not being able to have two players playing at the same time. So I learned that making a dict and storing variables for each player would allow me to have multiple games running at the same time. However my code ended up being a bit messy. What are some ways I could change this?
if ctx.author not in self.dictionaries:
self.dictionaries[ctx.author] = {
ctx.author: {
"self.player": ctx.author.id,
"self.chosen_word" : "",
"self.guessed_letters" : "",
"self.remaining_guesses" : 6,
"self.has_ended" : False,
"self.has_won" : False,
"self.used_letters" : "",
"self.own_word" : False,
"self.user": "",
"self.edit_word": ""
}
}
if ctx.author in self.dictionaries:
self.dictionaries[ctx.author][ctx.author]['self.player'] = ctx.author.id
self.dictionaries[ctx.author][ctx.author]["self.remaining_guesses"] = 6
self.dictionaries[ctx.author][ctx.author]["self.chosen_word"] = ""
self.dictionaries[ctx.author][ctx.author]["self.guessed_letters"] = ""
self.dictionaries[ctx.author][ctx.author]["self.has_ended"] = False
self.dictionaries[ctx.author][ctx.author]["self.has_won"] = False
self.dictionaries[ctx.author][ctx.author]["self.used_letters"] = ""
self.dictionaries[ctx.author][ctx.author]["self.own_word"] = False
self.dictionaries[ctx.author][ctx.author]["self.user"] = ""
self.dictionaries[ctx.author][ctx.author]["self.edit_word"] = ""
Basically every time a player plays a game they will get new values, if they are existing player those values get reset back to default. However it ends up me needing to call the dict over and over again. Is there a better way to implement this?
Thank You!
解决方案
Keeping how you manage your games, you have multiple ways of doing it:
- Using
enumerate
:
reset_values = [ctx.author.id, "", "", 6, False, False, "", False, "", ""]
for count, key in enumerate(self.dictionaries[ctx.author].keys()):
self.dictionaries[ctx.author][key] = reset_values[count]
- Using
zip
:
reset_values = [ctx.author.id, "", "", 6, False, False, "", False, "", ""]
for key, value in zip(self.dictionaries[ctx.author].keys(), reset_values):
self.dictionaries[ctx.author][key] = value
- Creating a blank model:
blank_model = {
"self.player": ctx.author.id,
"self.chosen_word" : "",
"self.guessed_letters" : "",
"self.remaining_guesses" : 6,
"self.has_ended" : False,
"self.has_won" : False,
"self.used_letters" : "",
"self.own_word" : False,
"self.user": "",
"self.edit_word": ""
}
self.dictionnaries[ctx.author] = blank_model
However, what you're doing is similar to object-oriented programming and I highly recommend you to use it. It will be way simpler to handle users and games than using dictionnaries to store data.
推荐阅读
- elixir - 字符串连接:二进制语法 v. 运算符 <>
- aws-sdk-js - 在茉莉花内运行时,aws-sdk-js 似乎无法正常工作
- python-3.x - 如何有效地分解数据?
- java - 为什么 Char 不会打印,并且计数器超出了我的循环;
- javascript - foo.x 的输出是什么
- r - 如何从数据框中选择与向量中的任何元素匹配的行?
- html - 如何在流中标记 pyparsing 位置以便稍后返回?
- python - Scrapy xpath无法提取
- python - 使用 .txt 文件中的 python 按日期和时间对行进行排序
- c# - 从三个vector3创建一个矩形网格