为什么它只循环 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 次,因为您正在迭代字符串 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

,

使用:

for x in range(1,100):

而不是:

 x = '100'
    for x in 'play'