Reputation: 9
I am making a project where the user can input a server and each server has a code. This project is gonna have 800+ servers so i'm looking for a optimal way of doing it. Currently im using if statements but I feel like theres a simpler and more efficient way of doing it.
This is my current code:
if (message_content[0] == bot_call):
server = message_content[1:]
if server not in server_list:
await channel.send("Invalid Server")
elif (server == "1"):
code = "73079"
elif (server == "2"):
code = "73127"
elif (server == "3"):
code = "73175"
elif (server == "4"):
code = "73054"
elif (server == "5"):
code = "328041"
elif (server == "6"):
code = "328039"
elif (server == "7"):
code = "447417"
Upvotes: 0
Views: 70
Reputation: 1
People suggest yo use dicts when you need to create multiple variables, but I understand your question differently. You are asking, If I am not wrong, what is the most efficient way to create all these 800 variables. In that case, the proper way to do so is using dicts, although you could also use global() attribute inside a loop (NOT RECOMMENDED). Your specific problem in here is that each code is different for each server, so you must define manually every code for every server. The computer can not guess which code the server will have.
Upvotes: 0
Reputation: 15488
Use a dictionary instead. First define it with:
servers = {
"1": "73079",
...
}
And so on. And assign with
code = servers[server]
Upvotes: 1
Reputation: 147
A python dictionary will be a good way to do that:
if (message_content[0] == bot_call):
server = message_content[1:]
if server not in server_list:
await channel.send("Invalid Server")
servers = {"1": "73079", "2": "73127", ...}
code = servers[server]
Upvotes: 0