python - 如何在Python中存储数据?

标签 python discord.py

我正在制作一款游戏,玩家需要为一定数量的金钱而工作。但我在不和谐服务器上运行代码,这意味着会有多个用户?如果我可以获得每个用户的唯一 ID,那么如何存储每个用户的信息?

这是我编写的一些代码,基本上反射(reflect)了我的另一代码。如果他们输入以前使用过的用户名,我如何存储资金以及退款。

def start():
        name = input("What is your name?")
        main()
def main():
        print('1. New Player')
        print('2. Work')
        answer = input()
        if answer == '1':
            start()
        if answer == '2':
            work()
    def work():
        print("You work for $100")
        money += 100
        main()
    main()

(抱歉,如果我的问题太宽泛...如果需要更多详细信息,请提出任何问题。)

最佳答案

一个简单的建议是创建一个类来描述玩家、他们的属性以及与玩家相关的任何功能。例如:

class Player:
    def __init__(self, id):
        self.money = 0
        self.items = []
        self.level = 1
        # Initialize other player attributes here

    def add_money(self, amount_added):
        self.money += amount_added

def main():
    player_1 = Player(id=1)
    player_1.add_money(100)
    player_2 = Player(id=2)

这是一个非常通用的示例,但是为玩家定义一个类将允许您轻松创建一个可以关联每个玩家的对象。如果你将 100 加到玩家 1 的钱上,那么这将完全独立于玩家 2 的钱。我建议搜索类(class)上的教程/示例,您就会有所感觉!

关于python - 如何在Python中存储数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622075/

相关文章:

python - 使用 aiohttp 在 Python 中从内存上传 multipart/form-data

python - 如何遍历数据帧的行并检查列行中的值是否为 NaN

python - 逐行读取文件并追加单词

python - 将文本输入到 codemirror python selenium 的文本区域

python - 正则表达式从字符串中获取数字

python - 参数错误: ValueError: 'params' arg (<class 'list' >) can be only a tuple or a dictionary

python - Discord Bot - "Attribute Error: ' NoneType' 对象没有属性 'strip.'

python - on_load 清除某个 channel 的所有消息

python - 如果我使用discord.utils.get()或discord.utils.find(),为什么我的成员变量没有被填充?

python - pygame可以检测屏幕的视频模式吗?