Introduction

One of my favourite pastimes is doing quizzes, so naturally while I was visiting my family for Christmas this year, I was keen for us to try doing some different quizzes. We tried a couple, but something that stuck out to me is that I have quite a weakness when it comes to Geography. To attempt to remedy this, I’ve created this short script to randomly give me a city from a list of 48,000 (you can find the list here) to do some research into. The idea is that I get a new city every day and try and see what I can find out about that city online.

Methodology

The list of cities that I found was stored as a csv file, and since I only wanted a simple implementation, I decided it was worth leaving it as one. I considered creating a database, but decided that keeping that database on my laptop was more effort than it was worth.

For the sake of convenience, I wrote this script in Python. The city is selected randomly while being read in from the csv file, and then the file is rewritten without the chosen city. The program also contains an option to restore the file to include all the cities it has originally.

I used the pyinputplus library to take the user input for the menu to save development time. I also opted to read in the csv file using Python’s csv library.

The code for the two menu options is as follows:

# Chooses a random city from the csv and then removes that city from the csv file
def choose_city():

    # Counts the number of rows in the csv
    # This is used to determine the number of cities remaining
    # It is used to randomly choose a city
    line_num = 0
    with open("worldcities_edited.csv", encoding='UTF-8') as file:
        reader = csv.reader(file, delimiter=",")
        for row in reader:
            line_num += 1

    # Uses a random number function to select a random row
    line = random.randint(0,line_num-1)

    # The rows aside from the chosen city are saved here
    # This is used to then save them back to the csv without the chosen city
    rows = []

    # Finding and printing the chosen city
    with open("worldcities_edited.csv", encoding='UTF-8') as file:
        reader = csv.reader(file, delimiter=",")
        for row in reader:
            # Printing the city if the line numbers match
            if reader.line_num == line:
                print("Today's city is " + row[0] + ".")
            # Saving the row to the list of rows if it is not the chosen city
            else:
                rows.append(row)

    # Writing the rows back into the file
    with open("worldcities_edited.csv", "w", encoding='UTF-8') as file:
        writer = csv.writer(file, delimiter=",")
        writer.writerows(rows)


# Adds all cities back into the file
def reload_cities():
    # Grabbing the cities from the original file
    with open("worldcities.csv", encoding='UTF-8') as file:
        contents = file.read()

    # Copying them back into the file used for editing
    with open("worldcities_edited.csv", "w", encoding='UTF-8') as file:
        file.write(contents)

If you have any interest in seeing the full project, you can see it here on my Github.

The Future?

This script was written very quickly to get the idea up and running, but I am considering future expansions. For instance, I have considered rewriting the code in Java and creating a basic GUI. I’m not sure if there is much actual benefit to this however, so unless I find a concrete motivation, I may also just leave it.