首页 > 解决方案 > 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!

标签: python-3.xdiscord.py

解决方案


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.


推荐阅读