Higher/Lower Game | Python
In today’s world, celebrity follower counts on social media are used as a gauge of status and rank amongst their peers. That’s why for my next Python project, I created a Higher/Lower Game that challenges the player to guess who has a higher Instagram follower count when presented with two options.
If guessed correctly, a point is added to the player’s score. The celebrity with the higher follower count is retained and presented against a new celebrity for comparison. Let’s take a look at how the code works…
First, let’s take a look at how our data for the game is formatted. Within a file called game_data, a list (‘data’) holds a dictionary for each celebrity. Each dictionary has four key/value pairs as seen above, for each of the pieces of data we will need.
Now, within the main.py file, I import ‘data’ from game_data as well as the ascii and random modules. I also imported system from the os module, to enable screen clearing.
Next, two variables are declared, ‘end_game’ which is set to the Boolean value False; and ‘score’ which is set to 0.
Finally, two functions are defined. The first is clear(), which I have compressed for visibility. However, if you are curious about this function, I have discussed it here. The other, a function named get_data() returns a random selection from the ‘data’ list by using random.choice().
Next, display() is defined, which handles the main presentation of the comparison game. The function first prints the ASCII header logo and the current streak. Next, the first celebrity is printed by pulling information stored in the ‘position_A’ variable (discussed later) by using the specific key to the data needed. For example, assuming the dictionary ‘Nicki Minaj’ is assigned to the ‘position_A’ variable:
‘position_A[“name”]’ can be used to extract ‘Nicki Minaj’ just as ‘position_A[“description”]’ can be used to extract ‘a musician’.
The ASCII logo for versus is then printed along with data stored in ‘position_B’ in the same manner as ‘position_A’.
Next, get_selection() is defined, which prompts the user to make a selection of either ‘A’ or ‘B’. Dependent on the choice made, variables ‘user_selection’ and ‘alt_option’ are assigned their appropriate values. If neither ‘A’ nor ‘B’ is input, a message advising of an invalid selection is printed and get_selection() is called to re-prompt the user for a selection.
The last function defined, named compare(), compares two values as the name suggests. Using an ‘if’ statement, ‘user_selection[“follower_count”]’ is compared against ‘alt_option[“follower_count”]’. If the follower count of the user’s selection is greater than the follower account of the alternate option, ‘score’ is incremented by 1. Then, ‘position_A’ is assigned with the value of ‘user_selection’ and ‘position_B’ is assigned with new data by being set to the value returned in get_data().
The function operates in this way, as the game retains the current celebrity with the highest follower count to be compared against a different celebrity until the current celebrity is ‘out followed’.
If the user’s selection is incorrect, the ‘else’ statement is triggered, which displays feedback regarding the incorrect selection along with their final score. The user is then prompted as to whether they would like to play again. If so, ‘score’ is set to the value of 0, ‘end_game’ is set to False, and ‘position_A’ along with ‘position_B’ are set to new values by calling get_data(). Otherwise, ‘end_game’ is set to True which pulls the game out of its loop.
After all of our function declarations, the actual game code is pretty obfuscated with only a few lines of code. First, ‘position_A’ and ‘position_B’ are assigned with values from the get_data() calls. This is our very first two celebrities for comparison being assigned to their variables for handling by the functions. Then, while ‘end_game’ is False, display(), get_selection(), and compare() are called.
While I do not personally place any value on someone’s follower count, I did find this little game a fun one to build. You can view and download the source code for this project on my GitHub by clicking here. Thank you for your time!