I want to make an macro clicker for games, but I dont know how to start and stop clicking loop by the same key on keyboard. Here is where I am:
def start_pvp_clicking():
bind_pvp_key = entry1.get()
pyautogui.PAUSE = 0.08
while True:
counter = 0
if keyboard.is_pressed(bind_pvp_key):
while counter == 0:
print("clicking")
if keyboard.is_pressed(bind_pvp_key):
while True:
print("not clicking")
break
When I press a key, loop is starting but I cant stop it and run again by the same key.
The counter won't be updated after each time in the loop. It will remain 0 forever so the program will run forever.
Just update the counter to counter + 1 to prevent infinity loop and change (while counter == 0) because that loop will stop after the counter is bigger than 0, and you have 3 while loops and the last one sucks, there is no need to add the last loop, in the first loop call (while True) it will run forever because True will remain True forever.
I recommended adding a parameter called gameTicks = 0.02, then just temporarily stopping functioning each gameTicks (0.02). If you did what I said, the program should work well.
Try something like this:
while True:
key_togle = 0
if keyboard.is_pressed(bind_pvp_key):
key_togle = 1
while key_togle:
print('Doing stuff')
if keyboard.is_pressed(bind_pvp_key):
key_togle = 0
print('Not doing stuff')
Explanation:
You need to manage how to get in and out of a loop, so basically add a variable for breaking a inner loop, and a condition to change the value of the variable.
How about this?
key_togle = 0
while True:
if keyboard.is_pressed(bind_pvp_key):
key_togle ^= 1
if key_togle:
print('do something')
Related
well i've a loop in which i want to enter only for once when condition is satisfied and then again it should enter after a variable value gets changed.
class OMS:
timeframe_check = 1
def upside_ex(self,tickers):
for ticker in tickers:
try:
order_placed = False
if self.timeframe_check == latest_time:
if order_check_1st_sca == True and not order_placed:
self.PlaceOptionOrderBuy(tickers,"buy",quantity)
order_placed = True
if order_check_1st == True and not order_placed:
self.PlaceOptionOrderBuy(tickers,"buy",quantity)
order_placed = True
else:
order_placed = False
self.timeframe_check += 1
print(self.timeframe_check , "this is timeframe_check from else which got incremented")
break
except Exception as e:
print(e)
pass
i want to enter inside the inside the if loop when both timeframe_check and latest_time are equal and make the order_placed as true so that it'll not enter inside the loop until and unless it's made false again.
But the issue is in the first iteration it's entering the if loop but when the second iteration comes it only enters the else loop and does the increment. after increment it should again enter the if loop but it's not doing it as expected.
Because at the same time it's shows timeframe_check as 1 and also inside the else loop timeframe_chcek as 2 after incremented but then why it's not entering the if loop after the incrementing.
you using break. So after getting incremental you are breaking out of the loop that's why next time loop doesn't iterate. You can use pass instead.
I want to loop from "while flag: ~~" to the end of the code, but when I don't put "break."
Only the last "for" phrase is repeated. How can I repeat the whole "while flag: ~~" part?
I'm really noob ;(
pyautogui.click(xxx,yyy)
colorlen = len(color)
for i in color:
print(i)
dif = 10
while flag:
pyautogui.moveTo(xxx, yyy)
pyautogui.click(xxx, yyy)
index = index + 1
iter_num += 1
img = ImageGrab.grab()
for i in range(left_top[0], right_bottom[0], 2):
if flag == True:
for j in range(left_top[1], right_bottom[1], 2):
rgb = img.getpixel((i,j))
if flag == True:
for ss in range(colorlen):
if(abs(rgb[0] - color[ss][0]) <= dif and abs(rgb[1] - color[ss][1]) <= dif and abs(rgb[2] - color[ss][2]) <= dif):
pyautogui.moveTo(i,j)
pyautogui.click(i+1,j+1)
pyautogui.moveTo(selx,sely)
pyautogui.click(selx,sely)
pyautogui.press('enter')
All the fors and the while loop are necesseraly repeated while the flag value is non empty.
If not, that means that something in your indented fors is looping on itself and "blocking" something.
Plus, your second if flag == True seems useless unless the flag value can be changed between your two ifs.
So check if something in your while loop isn't blocking the loop.
The fastest way to be sure is to log or print in while beginning
I'm working with while loops and I'm just a bit confused on how to break out of it in the way that I want. So I've nested a for loop within a while loop:
x = True
y = 0
while x:
if y >= 5:
x = False
print('break')
else:
for x in range(7):
y += 1
print('test')
The output that I'm looking for is 5 tests printed out and one break. However, every time I run the program it prints out 7 tests before it goes to the break. I'm not exactly sure, but I think I'm just confused about something within while loops! If someone could explain this to me please let me know :) I have found ways around this, but I'd like to get an understanding of why it doesn't work.
This is because it's performing the entire for loop within the while loop therefore y will become 7 before it checks again. Removing the for loop will resolve this.
x = True
y = 0
while x:
if y >= 5:
x = False
print('break')
else:
y += 1
print('test')
y = 0
while y < 5:
print("test")
y += 1
print("break")
Would work.
There is no point in having another variable like "x" for the while loop when it allows for you to set the condition directly.
Because inner loop will complete before the next iteration of the outer loop. I.e. once the inner loop starts it does all 7 iterations before starting the next iteration of the while loop.
You can do this by just using one loop. Print out “test” increase counter and put in an if condition to break when counter is 5.
The reason 7 tests print rather than 5 is that your entire for loop is executed before you go back to the beginning of your while statement. I think your understanding is that, after one iteration of a for loop, you go back to the beginning of the while loop, but this is incorrect: your for loop is executed completely before going back to the beginning of the while loop.
After you step into the for loop, you increment y 7 times and print test 7 times. y is now >= 5, and you go back into your if statement. The if statement turns x false, thereby "turning off" the while loop, and a break statement is printed. If you just want to print out 5 tests and one break, it would be much easier to simplify your code as such:
y = 0
while True:
if y < 5:
print('test')
y += 1
else:
print('break')
break
Try
i = 0
while True:
if i == 5:
break
print('test')
i = i + 1
I expected that in this If statement, the variable 'i' would increment until it eventually equals 10, and subsequently 'if 10 < 10' would return False, breaking my while loop. But this code seems print until 10 and then get stuck in an infinite loop unless I add an else: break. Why?
i=0
while True:
if i < 10:
i = i + 1
print(i)
while X repeats when X equals to True so in while True it's always True. It only breaks with break statement.
In your code, you only check the value inside the while loop with if so neither you break the while loop nor you change True to False in while True.
If you want to use while:
i = 0
while i < 10:
i += 1
print(i)
Or
i = 0
while True:
if i < 10:
i += 1
print(i)
else:
break
Without while:
for i in range(10):
print(i)
while True
will make the loop run forever because "true" always evaluates to true. You can exit the loop with a break.
To achieve what you want to do, I would use
while i < 10:
print (i)
i++
That is because there isn't anything telling you to terminate the loop. So it will continue even after the if statement isn't satisfied.
This is why it is generally not a great practice to use while True
You can achieve the same thing with a for loop when the break condition is built into the loop:
for i in range(0, 10):
print(i)
If you want to use while True then you can go for:
i=0
while True:
i = i + 1
print(i)
if i == 10:
break
I think you need to understand a few things here, as you have set while True which means statement will never gets false so there is never end to while loop even if if condition gets fail. So the while loop will continue running till you interrupt.
The only way you can achieve this without break is like this, where you have a variable which will reset the condition of while loop to false when if loop fails
i=0
condition = True
while condition:
if i<10:
i=i+1
print(i)
else:
condition=False
My problem - and I don't know why there is a keyword continue, which should leave the value of 3 and go further. In fact, I have a loop that is infinite - that is, it crashes the program.
tab = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
i = 0
while i < len(tab):
print(tab[i])
if tab[i] == 3:
continue
i+=1
It looks like you are trying to iterate through the list until you find a 3 then break. Do something like this:
items = [1,2,3,4,5]
for item in items:
if item == 3:
break
The keyword continue will pass to the next iteration of the loop, where break will stop the loop.
The continue keyword continues with the next iteration of the loop.
In your case, it prevents the statement i+=1 to be executed.
Here is what happens:
Loops through 0,1,2 just fine
When it evaluates tab[i] = 3 it proceeds with the next iteration of the loop and i+=1 is never executed, hence i remains 3 and never gets incremented. It keeps doing this forever.
If you want to exit the loop, you can use the break statement instead of continue.
For more information, you can read on the continue keyword here: https://docs.python.org/3/tutorial/controlflow.html
I think the answers here all assume that OP wanted to use break instead of continue.
However, if you need to use continue in a while loop, make sure you have an increment/decrement operation above the continue statement, because the continue statement jumps to the top of the while loop and, in your case, i remains 3.
Respectively, the continue statement in a do-while loop jumps to the bottom of the loop. So, in your case you have two options, either you increment above the continue or use a for-loop.
tab = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
i = -1
while i < len(tab):
i += 1
print(tab[i])
if tab[i] == 3:
continue
OR
for i in range(0,15):
if i == 3:
continue
print(i)
You're using the continue keyword, but I think you want break.
Your code is running forever because i never iterates past 3. Once i == 4, it enters your if statement and continues the loop. Since it continues, it never iterates after that.
Because you are using the continue keyword that skips the rest of the code.
Once i reaches 3, it skips over the i+=1 command. It appears that you want to use the keyword break. You could also do:
for i in tab:
print(i)
if i == 3:
break
If you are trying to skip the value "3" then
tab = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
i = 0
while i < len(tab)-1:
i+=1
if tab[i] == 3:
continue
//i+=1 => infinite loop
print(tab[i])