DEV Community

Alexyy
Alexyy

Posted on

Pyautogui Country Guesser πŸŒπŸ€–

Are you trying to learn python? Or are you just struggling with some geography assignment about countries? Well this is the tutorial for you!

1. Importing The Packages
Before we start making this thing, Lets start with importing some packages! We of course need Pyautogui installed!

py -m pip install pyautogui
Enter fullscreen mode Exit fullscreen mode

2. Creating The Program
Lets start with making a simple while loop to make a little interactive menu!

import pyautogui

def main():
    while True:
        command = input("Enter a command: ")
        if command.lower() == "start":
            pass
        if command == "exit":
            print('Exiting...')
            break
        else:
            print("Invalid Command!")

main()
Enter fullscreen mode Exit fullscreen mode

3. Adding the data
Lets add some country names into the script, Create a text file with a cool name! For this tutorial, I named the text file countries.txt. Copy and paste the contents in this Repository into your txt file!

4. Getting all the data

We can loop through the data inside of the file we created with the open function!

def main():
    while True:
        command = input("Enter a command: ")
        if command.lower() == "start":
            time.sleep(1) #Give some time for the user to select the field for entering the country name
            print("Starting...")

            data = open("./countries.txt", "r") #Open the file

            for j in data: #Get everything inside of the txt file
                pyautogui.typewrite(j)
                pyautogui.press("enter") #Press Enter in the keyboard

            print("Done!") #Finished process!
        if command == "exit":
            print('Exiting...')
            break
        else:
            print("Invalid Command!")
Enter fullscreen mode Exit fullscreen mode

Congratulations!
You created your own little automated Pyautogui program! Thanks for reading, Comments are opened for critics and suggestions!

Top comments (0)