Home > Back-end >  When I use While Loop my Code no Longer works
When I use While Loop my Code no Longer works

Time:08-05

import pyautogui
import pydirectinput
import time


x = 1
while x <190:
 for number in range(190):
  time.sleep(0.1)
  pyautogui.click(480, 595)
  x = x 1
  if x == 190:
  

   pydirectinput.keyDown('shiftleft')
   time.sleep(2.5)
   pydirectinput.keyUp('shiftleft')
   x = 1 

When I run this all it does is repeat the first code and does not activate the second code I am a newb at coding so I don't know why this is happening. It just activates the 2nd "x = 1" instead of doing the code above it first then resetting the loop. I want the code to run indefinitely but run both codes instead of only the click code

What should I put in after "if x == 190:"

CodePudding user response:

A while loop executes the code inside its body as long as its condition evaluates to true. Your condition is whether x is equal to 1 or not.

You initialize x to 1, but you never change its value in your while loop's body. So, the condition is always true, and you have an infinite loop.

You need to modify the value of x inside your while loop to break out of the loop.

CodePudding user response:

import pydirectinput
import time


x = 1
while x == 1:
 for number in range(190):
  time.sleep(0.1)
  pyautogui.click(480, 595)

 pydirectinput.keyDown('shiftleft')
 time.sleep(2.5)
 pydirectinput.keyUp('shiftleft') ```
  • Related