亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

for in 語句只打印最后的結果

for in 語句只打印最后的結果

千巷貓影 2023-10-31 15:59:53
所以我正在使用可以查看機器人的命令進行測試。它將檢測 arg 并查找 arg 是否在事物中,如果在事物中它將打印以下信息。但是,它僅適用于最后的信息。我該如何修復它?命令代碼:@client.command(pass_context=True)async def viewbot(ctx, arg):            for bot in data['bots']:                name = bot["name"]                #createdby = bot["createdby"]                briefdesc = bot["briefdesc"]                desc = bot["description"]                slug = bot["slug"]            if arg == f'{slug}':                embed = discord.Embed(title=name,description=briefdesc,timestamp=ctx.message.created_at,colour=discord.Color.dark_green())                await ctx.send(embed = embed)            else:                print("no")JSON 代碼:{    "bots": [        {            "name": "Bot1",            "createdby": "Bit#0258",            "briefdesc": "Texts.",            "description": "Test",            "library": "discord.py",            "slug": "bot1"        },        {            "name": "Bot2",            "createdby": "BotTest#0001",            "briefdesc": "Text",            "description": "Rext",            "library": "discord.js",            "slug": "bot2"        }    ]}如果您知道如何修復它,請回復此問題。謝謝。
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

看看你的邏輯:


        for bot in data['bots']:

            name = bot["name"]

            #createdby = bot["createdby"]

            briefdesc = bot["briefdesc"]

            desc = bot["description"]

            slug = bot["slug"]


        if arg == f'{slug}':

            ...

您特別告訴它遍歷所有可用的機器人,提取它們的字段,并將每個字段寫入前一組數據的頂部。當您離開循環時,每個變量中只有最新值。這就是變量的工作原理。


要獲取所有值,請將處理(if)移至循環內,或將數據收集在列表中而不是標量變量中。


查看完整回答
反對 回復 2023-10-31
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號