2

I'm completely new to programming and python as a whole but I have recently joined a class. Currently I am trying to make a program which prompts the user for an input of the coordinates of the center of an object on the screen.

The program will then take the X and Y coordinates of the item and randomize the coordinates within a range of +- 15. So if the X coordinate is 30, the program will pick a random number of 15 to 45.

Then the program will take a random delay between 3.2 to 4.3 seconds and move the mouse from it's current position to the randomized coordinates within the random time delay.

I want it to be able to loop endlessly until I prompt it to stop, and I think I can figure that out. However, I do not understand how to properly use parameters to allow the local variables coords_x , coords_y and click_delay to be used in the function main()

Here is what I have so far:

#! python3
import pyautogui, sys
import random
from random import randint

center_x = int(input("Please enter the X coordinate of the center of the item:"))
center_y = int(input("Please enter the Y coordinate of the center of the item:"))

#y = 23 long 735 - 712
#x = 23 862 - 838

buffer = 17

def randomizex():
    min_x = center_x - buffer
    max_x = center_x + buffer
    coords_x = randint(min_x, max_x)


def randomizey():
    min_y = center_y - buffer
    max_y = center_y + buffer
    coords_y = randint(min_y, max_y)


def randomizedelay():
    click_delay = random.uniform(3.2, 4.3)

def main():
    randomizex()
    randomizey()
    randomizedelay()
    pyautogui.moveTo(coords_x, coords_y, click_delay)

main()

I appreciate any help. The other answers I have found to questions like these are rather confusing to a newbie and some of them are for python 2.

2 Answers 2

1

Welcome to SO (and to programming)!

You are almost there with this, the thing you are missing is saving the return values of the randomizex(), randomizey(), randomizedelay() functions to variables, so they can be used within main. Even though you name the variables within their respective functions, that naming does not go beyond the scope of these functions, so main has no idea they are called that. Something like this should work:

def main():
    coords_x = randomizex()
    coords_y = randomizey()
    click_delay = randomizedelay()
    pyautogui.moveTo(coords_x, coords_y, click_delay)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Managed to fix it had a bunch of minor errors as well.
0
def main():
    your_x_coords = randomizex()
    your_y_coords = randomizey()

Your functions return x_coords, so you have to assign them to another local variable inside of main.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.