while True: try: rev = rev_msg() print(rev) if rev is None: continue elif rev.get("post_type") == "message": except Exception as e: print("Error occurred:", e) # 记录异常信息 continue if rev["post_type"] == "message": if rev["message_type"] == "private": # 私聊 if '在吗' in rev['raw_message']: qq = rev['sender']['user_id'] # 获取消息发出者的qq号 send_msg({'msg_type': 'private', 'number': qq, 'msg': '我在'}) # 发送 elif rev["message_type"] == "group": # 群聊 group = rev['group_id'] if '在吗' in rev['raw_message']: qq = rev['sender']['user_id'] send_msg({'msg_type': 'group', 'number': group , 'msg': '我在呢'}) else: continue else: #rev["post_type"]=="meta_event": continue找出错误的问题
时间: 2024-02-12 14:02:52 浏览: 51
PYTHON学习教程:条件判断(if、elif.、else代码练习print Run).docx
您的代码存在缩进错误,在 `if` 语句和 `except` 语句之间应该加一个冒号 `:`。同时,最后的 `else` 语句也存在缩进错误,应该和 `if` 语句处于同一缩进级别。
正确的代码应该是这样的:
```
while True:
try:
rev = rev_msg()
print(rev)
if rev is None:
continue
elif rev.get("post_type") == "message":
if rev["message_type"] == "private": # 私聊
if '在吗' in rev['raw_message']:
qq = rev['sender']['user_id'] # 获取消息发出者的qq号
send_msg({'msg_type': 'private', 'number': qq, 'msg': '我在'}) # 发送
elif rev["message_type"] == "group": # 群聊
group = rev['group_id']
if '在吗' in rev['raw_message']:
qq = rev['sender']['user_id']
send_msg({'msg_type': 'group', 'number': group, 'msg': '我在呢'})
else:
continue
else: # rev["post_type"]=="meta_event"
continue
except Exception as e:
print("Error occurred:", e) # 记录异常信息
continue
```
阅读全文