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:"
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.
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
I've created a program that uses pyautogui to locate an image stored in the directory and click on it when it finds it. So when it sees 'Microsoft Edge' it instantly clicks on it. I have this running on a loop and want to stop the loop when it finds the image. This is really important for me—but I want the loop to stop even before it clicks it, so while 'double' is False, as you will see below.
Here it is:
import pyautogui as p
import time
import random
import os
import pygame
from hashlib import sha256
def search(query):
p.hotkey("ctrl","e")
time.sleep(.1)
p.write(query)
p.press("enter")
time.sleep(.67)
def imageFind(image,g,double):
a = 0
b = 0
while(a==0 and b==0):
try:
a,b = p.locateCenterOnScreen(image,grayscale=g)
except TypeError:
pass
if double == True:
p.doubleClick(a,b)
else:
p.click(a,b)
return a,b
#p.hotkey("win","d")
counter = 1
while counter == 1:
image_find = imageFind("Edge.png",False,False)
if image_find == True:
imageFind("Edge.png",False,True)
counter = 0
I've done if image_find == True but it doesn't work as the loop continues.
So how do I code this so that when it finds the image, it stops. How do confirm that what it finds is a True statement?
imageFind does not return a single bool values but instead returns a tuple. your True condition is when both a and b are non zero so change your if image_find == True: to if all(image_find):
all() returns True only if all values are True or non zero
Or try to check if the minimum is nonzero:
if min(image_find):
I tried to make a 'random' text program with 2 possible outputs, randomization works but range() isn't working. I always make some stupid mistakes so don't go hard one me over some stupid small thing pls
import time
import pyautogui
import random
time.sleep(2)
for i in range(50):
if int(random.randint(1,2)) == 1:
pyautogui.typewrite('bruh1')
pyautogui.press('enter')
random.randint(1,2)
elif int(random.randint(1,2)) == 2:
pyautogui.typewrite('bruh2')
pyautogui.press('enter')
random.randint(1,2)
x is the random number returned by random.randint(). Not sure calling x() is the right thing. Can you please have a closer look at that part?
import time
import pyautogui
import random
def typewrite(text):
pyautogui.typewrite(text)
pyautogui.press('enter')
for _ in range(50): # I use _ for variable if don't use it
x = random.randint(0,1) # return 0 or 1 of type int
if x: # if x is 1
typewrite('bruh1')
else: # if x is 0
typewrite('bruh2')
This is the code that returns the value I expect:
python
def function1():
highest = 0
for x in range(100):
for y in range(100):
if x+y > highest:
highest = x+y
return highest
print(function1())
This code prints "198".
Now, if I indent the return statement under the if statement, like this:
def function1():
highest = 0
for x in range(100):
for y in range(100):
if x+y > highest:
highest = x+y
return highest
print(function1())
The code prints "1".
Why? What's happening behind the scenes?
Sorry if this is a trivial question, but don't know the basic structure and stuff, I'm only learning by experimenting...
it is terminated in the second step of the second loop.
highest = 0
x = 0
y = 0
if 0+0 > 0 (false):
increment y to 1:
the second run:
highest = 0
x = 0
y = 1
if 0+1 > 0 (true)
highest = 1
return # return command is called and therefore the execution of the function ends
A return statement ends the execution of the function call and "returns" the result.
The return statement stops a loop at the point when you call it. So in the first example it goes through the whole for loop and then returns the value of highest. In the second example it returns on the first entry of the for loop, so it doesn't go through the whole for loop.
A return statement ends the function. If you put it inside the if statement, the function stops as soon as the condition is true. So it will return the first value of x+y that's more than 0, which happens when x = 0 and y = 1, rather than continuing to look for higher values.
In the first example, the return statement is below both for loops, but is also indented outside of both of them. That means that it isn't reached until the nested for loop expression is evaluated for every possible combination of x and y.
On the other hand, in the second example, return is indented inside the if statement. Once the if statement is reached, the Python interpreter reads all lines indented at that level, including return highest. So the first time x+y > highest (which is when x+y==1, the if statement runs, and the function returns 1. The return acts as an early break, so even though there will be more values that it could process, it doesn't matter because you explicitly told it to return early.
for i in xrange(5):
abc()
time.sleep(3)
print('?~~~~~')
print('~?~~~~')
print('~~?~~~')
print('~~~?~~')
print('~~~~?~')
print('~~~~~?')
print('?~~~~~')
print('~?~~~~')
print('~~?~~~')
print('~~~?~~')
print('~~~~?~')
print('~~~~~?')
In this answer I am assuming that you want to move the question mark along the line as the timer advances, with a three second gap in between.
This code nearly needs to be rewritten completely. Don't fret, it's not too bad and it's easy to fix.
The first thing that is wrong here is that you haven't imported your time module for time.sleep(). To do this you must simply add at the top of the code import time. Secondly, you do not need to use abs() in your code, and Python doesn't understand it. Thirdly, when you use a for loop, or any loop for that matter, it executes the whole thing one iteration, then goes back to the start for the next, so your timer is just printing all of that every time it goes through. It does not wait to make it flow, it just prints a block of the tildes every three seconds.
At your current level, I would use a simple if and a variable to test for which iteration it is, because you might not know how to splice yet. Here it is:
import time # importing time so time.sleep() works properly
iteration = 1 # Code to tell where the question mark is
for i in xrange(5):
time.sleep(3)
if iteration == 1: # asking if the variable is 1
print('?~~~~~') # printing this
iteration += 1 # adding one to the iteration variable
elif iteration == 2:
print('~?~~~~')
iteration += 1
elif iteration == 3:
print('~~?~~~')
iteration += 1
elif iteration == 4:
print('~~~?~~')
iteration += 1
elif iteration == 5:
print('~~~~?~')
iteration += 1
elif iteration == 6:
print('~~~~~?')
iteration = 1
Here is the version using splicing
import time
iteration = 1
list = list('~~~~~~')
old_list = list
for i in xrange(5):
time.sleep(3)
list[iteration-1] = '?'
print("".join(list))
list = list('~~~~~~')
if iteration == 6:
iteration = 1
else:
iteration += 1
If you want a tutorial, that I would recommend is the one at http://tutorialspoint.com/python/
Otherwise, the original Python tutorial is great: https://docs.python.org/2/tutorial/
I am doing a math problem that requires me to multiply numbers and check to see if they are palindromes.
import sys
sys.setrecursionlimit(1000000)
import time
def values():
x=999
y=999
product=0
generator(x,y,product)
def generator(x,y,product):
while x >= 900:
product=x*y
strp=str(product)
check=strp[::-1]
print (check)
time.sleep(0.1)
if strp==check:
print ("done")
x=x-1
else:
y=y-1
generator(x,y,product)
values()
I am using Mac, and it goes through the loop a couple of times but then displays a "Pytho quit unexpectedly" error.
Your program is crashing because your recursion loop doesn't stop. When x reaches the value of 900 the generate function always calls the else branch of its code. You reed to add a condition for the loop to stop. Otherwise it fills up the memory, making the program crash because recursion loops have a limit on how many times you call them.
As per the answer above, your recursion never stops because once x = 900 it always recurses by calling the else code.
I suggest the following solutions:
a) If you're interested in keeping y at 999 until x is 900 and then decrease y until it's 900 you should add the following to your else (i.e. do 999x999, 999x998... 999x900 ... 998x900 ... 900 x 900):
else:
if y >= 900:
generator(x,y,product)
y=y-1
b)If you want to recurse on both of them (i.e. decrease them in parallel):
def generator(x,y,product):
if x >= 900 and y >=900:
product=x*y
strp=str(product)
check=strp[::-1]
print (check)
time.sleep(0.1)
if strp==check:
print ("done")
x=x-1
y=y-1
generator(x, y, product)
Personally I would recommend the second solution as it's neater.
Please note that when recursing on both of them you don't need to have while loops, an if check is enough.