【发布时间】:2021-09-07 20:11:21
【问题描述】:
我正在尝试使用 Python 编写不和谐的机器人,我遇到了这个机器人并将其拼凑在一起。
import discord
import asyncio
import random
client = discord.Client()
inEmail = input("Email:")
inPassword = input("Passwd:")
async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel("************")
messages = ["Hello!", "How are you doing?", "Testing!!"]
await client.send_message(channel, random.choice(messages))
await asyncio.sleep(120)
client.loop.create_task(background_loop())
client.run(inEmail, inPassword)
然而,当我尝试运行它时,我收到了SyntaxError:
File "1.py", line 7
async def background_loop():
^
SyntaxError: invalid syntax
这是为什么呢?在我测试它之前,我从未收到过它。
【问题讨论】:
-
在函数定义前使用
async仅在 Python 3.4 或更高版本中有效。 -
是的,我一直在使用“python3 1.py”运行脚本
-
我明白了,所以 python 3.4 以上的任何东西都应该工作吗?这真的很奇怪,因为我的服务器在一夜之间以某种方式更改了 python 版本:P
标签: python syntax async-await discord discord.py