【问题标题】:For loop not executing properly, not appending to listFor循环未正确执行,未附加到列表
【发布时间】:2018-02-20 02:42:42
【问题描述】:

我正在尝试为个人项目创建一个不和谐的机器人,以提高自己在 Python 编程方面的能力。但是,我遇到了一个问题。当机器人得到这段代码时:

def bloodbath(tribs):
    event = "null"
    for person in tribs:
        bloodbath_type = ["bloodbath_solo","bloodbath_duo"]
        output = ["Bloodbath Day."]
        type = random.choice(bloodbath_type)
        if type == "bloodbath_solo":
            output.append((random.choice(bloodbath_solo)) % (person))
        else:
            if len(current_duo) < 2:
                current_duo.append(person)
                if len(current_duo) == 2:
                    output.append((random.choice(bloodbath_duo)) % (current_duo[0], current_duo[1]))
                    current_duo.clear()
        return output

await self.bot.say(bloodbath(tributes))

出现了一些问题。 首先:for person in tribs 循环只运行一次,或者根本不运行。这是一个输出示例。

https://imgur.com/gallery/gIhnS

我想要发生的是让循环附加到输出列表 6 次或更少(取决于是否有需要 2 人的情况)并将其返回给机器人,在那里它可以将其输入到聊天中。

这是整个代码的一个hastebin。

https://hastebin.com/lumekazula.py

【问题讨论】:

  • tributes 的值在别处定义。我们不知道它的数据类型和大小。向我们展示该值的示例或生成它的代码。
  • 您好,Devin,下次您可能想了解Minimal, Complete, and Verifiable Examples。转储整个程序通常是不受欢迎的。

标签: python


【解决方案1】:

return output 行打破循环并立即返回。为了更好地理解这个问题,试试这个:

def spam():
    for item in [1, 3, 5, 7, 9, 11]:
        return item * 2

print(spam())  # -> 2

output = []return output 移到循环外部。剩下的你应该能弄清楚。

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多