Python 如何防止一个人做超过一定百分比的事情?

Python 如何防止一个人做超过一定百分比的事情?,python,bots,discord,Python,Bots,Discord,我知道问题的标题很清楚,但我不太知道如何表达。所以,我的问题是,我是一个开发团队的一员,这个团队是一个刺激股票市场的discord机器人。这个函数的作用是允许一个人购买其他人的股票。我该怎么做才能让一个人买不到某个人超过一定百分比的股份,比如说10%的股份 编辑: 添加了更多相关代码 def create_buy_embed(percent, user_mention): if percent >= 1: displayed_percent = 100 el

我知道问题的标题很清楚,但我不太知道如何表达。所以,我的问题是,我是一个开发团队的一员,这个团队是一个刺激股票市场的discord机器人。这个函数的作用是允许一个人购买其他人的股票。我该怎么做才能让一个人买不到某个人超过一定百分比的股份,比如说10%的股份

编辑: 添加了更多相关代码

def create_buy_embed(percent, user_mention):
    if percent >= 1:
        displayed_percent = 100
    else:
        displayed_percent = round(percent * 100, 3)
    return discord.Embed(
        description=f"You've bought {displayed_percent}% of {user_mention}"
    )

根据您提供的信息,我假设百分比是一个通常为0.0-1.0的值。假设是这样,这里有一种方法:

def create_buy_embed(percent, user_mention):
    if percent >= 0.1:
        return discord.Embed(
            description=f"You can't buy that much!"
        )
    else:
        displayed_percent = round(percent * 100, 3)
    return discord.Embed(
        description=f"You've bought {displayed_percent}% of {user_mention}"
    )

请注意,此输出背后的任何数学处理都不会在此处进行,因为这似乎只是给用户提供了一些输出。

预测交易后的库存。如果不允许这样做,则显示错误消息并中止事务。否则,继续事务并应用结果。而且,我看不出这个函数中的任何东西是如何保留每个用户有多少库存的。一旦函数返回,信息就会丢失。它会更改消息,但不会阻止他们这样做。我添加了更多相关的代码,我发现这些代码可能会有所帮助,这就是我的意思,这里并没有发生数学。你需要做的是找出购买发生的确切位置-可能是函数调用,可能是值重新分配-然后考虑在%值太大时阻止购买发生的最佳方法。根据您发布的更新,这里有一个提示:您是否有办法将用户想要购买的金额与可用的总金额进行比较?
def create_buy_embed(percent, user_mention):
    if percent >= 0.1:
        return discord.Embed(
            description=f"You can't buy that much!"
        )
    else:
        displayed_percent = round(percent * 100, 3)
    return discord.Embed(
        description=f"You've bought {displayed_percent}% of {user_mention}"
    )