python - 为什么它只循环4次?
问题描述
为什么我的命令只循环 4 次,但我将 x 设置为 100 次而不是 4。有人可以帮我解决这个问题!我不明白这个错误,我需要修复到星期五
import discord
from discord.ext import commands, tasks
import asyncio
import keyboard
import time
from pynput.mouse import Button, Controller
from pynput.keyboard import Key, Controller
from pynput.keyboard import Controller
from pynput import mouse
import mouse
TOKEN = 'mytoken...'
client = commands.Bot(command_prefix = '.')
@client.command(name='play')
async def play(ctx):
await ctx.channel.send("Hello! welcome to a game where you play my game through discord messages! How yo play: if you want to go write - 'go', left - 'left', right - 'right', back - 'back', sprint 'lgo', click - 'click', jump - 'jump'")
x = '100'
for x in 'play':
msg = await client.wait_for('message')
response = (msg.content)
print(response)
if response == "go":
keyboard.press('w')
time.sleep(2)
keyboard.release('w')
if response == "left":
keyboard.press('a')
time.sleep(2)
keyboard.release('a')
if response == "right":
keyboard.press('d')
time.sleep(2)
keyboard.release('d')
if response == "back":
keyboard.press('s')
time.sleep(2)
keyboard.release('s')
if response == "jump":
keyboard.press('b')
keyboard.release('b')
if response == "click":
mouse.click('left')
if response == "lgo":
keyboard.press('shift + w')
time.sleep(5)
keyboard.release('w')
client.run(TOKEN)
我正在制作一个不和谐的机器人,它可以通过不和谐的消息玩我的游戏,但是我有一个奇怪的错误软件,它循环了 4 次而不是 100 次
解决方案
它只循环 4 次,因为您正在迭代 string play
。
x = '100'
for x in 'play':
print(x)
这输出:
p
l
a
y
(并完全忽略 100,因为它在循环中被覆盖)
要循环 100 次,请使用:
x = 100 # number, not string
for i in range(x):
print(i)
这输出:
0
1
2
3
...
98
99
并x
保持值 100
推荐阅读
- testing - Puppeteer Chromium,禁用“匿名化 WebRTC 公开的本地 IP”
- assembly - Cortex-m4 asm 与 cortex-m0 asm
- python - 在 docker run 和 build 之间获取不同的 python 版本
- raspberry-pi - 无法访问 Raspberry Pi 上的 Jupyter 服务器
- php - 如何在 PHP 中使用 echo 在 img src 字符串的中间插入变量
- typescript - 作为变量给出时获取选项的打字稿错误
- c++ - 如何在 C++ 中使用套接字 (UDP) 在 LAN 上查找服务器
- vue.js - 计算属性未触发 v-if 块
- ms-word - 尝试使用 InsertHtml 方法将 html 插入内容控件时,在 context.sync() 出现 Rich Api 错误
- python-extensions - 如何在 Python 调试环境中构建扩展?