Rockborne’s Guess Who Game

by Metha Prathaban

03 Aug '21

In this blog, Rockborne Consultant Metha Prathaban discusses some of the fun educational “warm-down” activities that the cohort have been doing while on our programme.



At the end of almost every day during our virtual working period, we did “warm-downs”; they often involve answering fun prompts such as “What is your best memory?” and “Tell me your favourite joke!”; we also mix these in with things like ‘key learnings from the day’, and so on. These have led to some serious cohort integration and bonding over the weeks, with people sharing cherished memories from childhood, hilariously mortifying tales from their travels, and top-quality philosophical musings such as, “I’m grateful to be alive because it’s better than being dead” … But by far my favourite warm-down to date has been the “baby photo” one – each of us was asked to submit a photo of ourselves from early childhood, and together we guessed to whom each adorable photo belonged.



Around the same time, we began learning Python together – for some, this was a recap of previous knowledge and for others, this was an entirely new challenge. As a way to spice things up for those who had some prior experience of coding in Python, our trainer, decided to create a fun challenge for us; they added all our baby photos into a folder and included in this folder a skeleton code to read in these images and ask the user to input a guess for who they thought was pictured in a randomly generated image. Our job was to improve this code’s functionality by making it case-insensitive, asking the user to guess the age of the baby too, and any other improvements we could think of



The code uses the iPython.display module to display each image and the os module to interact with the Windows system. We begin by using os.listdir() to add all the PNG and JPG files in the folder to a list. We then use the random module to select a random image and display it. The code then asks for a user input to guess who in the cohort is pictured in the randomly generated image and compared the entered string to the filename (minus the last four characters, which are always ‘.png’ or ‘.jpg’). Within an ‘if’ statement, it makes the string comparison and returns a string telling the user whether they are right.



With the code in its current form, it is case-sensitive to the user’s guess, and only acknowledges a correct guess if the user uses the same capitalisation as in the filenames (which are themselves inconsistent in their capitalisations). This can be solved simply by suffixing the user’s guess and filenames with the ‘.lower()’ command before checking for string equality. Another adjustment made to this code was to allow nicknames of those pictured as valid guesses. In order to achieve this a dictionary was created, with the key being a nickname and the corresponding value being that person’s name as it appears on the filename. To enable users to guess a person’s age in their childhood photo, a separate age dictionary was created and another ‘if’ statement below the first was coded in to compare the user’s guess to the actual age of the child in the photo.



The final adjustment I wanted to include was a way to detect when the player was trying to input the correct name, but had misspelt it, and get the code to tell them that it was a misspelling. There are several ways to achieve this, but one simple way is to compare the sets of the input string and filename string. If the intersection of these two sets is above a certain size, then it’s possible/likely that the user has entered a misspelling of the correct name. After some experimentation, I decided that if the intersection of the two sets had a length greater than 75% of the filename string length AND the input string was no more than three characters longer than the filename string, then the code ought to tell the user that they’ve entered a misspelling and ask them to enter their guess again. This accounts for misplaced double letters and common misspellings of names. For example, if the player were to enter Elena’s name as ‘Eleanor’, the code would tell them that this is a misspelling of the correct answer. Similarly, if someone were to enter ‘Wasseem’ instead of ‘Waseem’, it would return a similar output string.



An example of the final game, with the outputs and user guesses, is pictured below.

Have you read our blog on what our consultants found their first week? Read that blog HERE


Twitter logo icon LinkedIn logo icon