首页 > 解决方案 > 为什么它只循环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 次

标签: pythonloopsdiscordcycle

解决方案


它只循环 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


推荐阅读