GITHUB REPO | FOLLOW ME ON -> TWITTER | LINKEDIN

Higher Lower Game in Python: TV Show Ratings

If you’re a TV fan who takes pride in knowing all the latest episodes, you’ll enjoy this Python program. I created a new version of the higher-lower game that’s based on TV show ratings. I’ll walk you through the process of building and enjoying this game in this article. Let’s get started!

Al-Junaed Islam

--

ascii art of higher lower game
ASCII of Higher-Lower

But first you can try this game by pressing the run/play button from below.

Repl.it

Step-by-Step Code Explanation

The first thing we need to do is set up our Python Higher-Lower Game. Open up your preferred code editor, create a new file, and save it with a descriptive name such as “higher_lower.py.”. Also, create other files which will be used as modules game_art.py, data.py.

game_art.py

logo = """
_ _ _ _
| | | (_) __ _| |__ ___ _ __
| |_| | |/ _` | '_ \ / _ \ '__|
| _ | | (_| | | | | __/ |
|_| |_|_|\__, |_| |_|\___|_|
| | __|___/ _____ _ __
| | / _ \ \ /\ / / _ \ '__|
| |__| (_) \ V V / __/ |
|_____\___/ \_/\_/ \___|_|
"""

vs = """
_ __
| | / /____
| | / / ___/
| |/ (__ )
|___/____(_)
"""

ASCII art of logo and vs.

data.py

tv_series = [
{
'name':'Breaking Bad',
'year':'2008-2013',
'genre':'Crime, Drama, Thriller',
'rating':9.5
},
{
'name':'Band of Brothers',
'year':'2001',
'genre':'Drama, History, War',
'rating':9.4
},
{
'name':'The Wire',
'year':'2002-2008',
'genre':'Crime, Drama, Thriller',
'rating':9.3
},
{
'name':'Game of Thrones',
'year':'2011-2019',
'genre':'Action, Adventure, Drama',
'rating':9.2
},
{
'name':'The Sopranos',
'year':'1999-2007',
'genre':'Crime, Drama',
'rating':9.19
},
{
'name':'The Twilight Zone',
'year':'1959-1964',
'genre':'Drama, Fantasy, Horror',
'rating':9.1
},
{
'name':'Better Call Saul',
'year':'2015-2022',
'genre':'Crime, Drama',
'rating':8.9
},
{
'name':'True Detective',
'year':'2014-Present',
'genre':'Crime, Drama, Mystery',
'rating':8.89
},
{
'name':'Friends',
'year':'1994-2004',
'genre':'Comedy, Romance',
'rating':8.88
},
{
'name':'Fargo',
'year':'2014-2023',
'genre':'Crime, Drama, Thriller',
'rating':8.87
},
{
'name':'Seinfeld',
'year':'1989-1998',
'genre':'Comedy',
'rating':8.86
},
{
'name':'Peaky Blinders',
'year':'2013-2022',
'genre':'Crime, Drama',
'rating':8.8
},
{
'name':'Fawlty Towers',
'year':'1975-1979',
'genre':'Comedy',
'rating':8.79
},
{
'name':'The Marvelous Mrs. Maisel',
'year':'2017-2023',
'genre':'Comedy, Drama',
'rating':8.7
},
{
'name':'House',
'year':'2004-2012',
'genre':'Drama, Mystery',
'rating':8.69
},
{
'name':'House of Cards',
'year':'2013-2018',
'genre':'Drama',
'rating':8.68
},
{
'name':'Rome',
'year':'2005-2007',
'genre':'Action, Drama, Romance',
'rating':8.67
},
{
'name':'Gomorrah',
'year':'2014-2021',
'genre':'Crime, Drama, Thriller',
'rating':8.66
},
{
'name':'Sons of Anarchy',
'year':'2008-2014',
'genre':'Crime, Drama, Thriller',
'rating':8.6
},
{
'name':'Boardwalk Empire',
'year':'2010-2014',
'genre':'Crime, Drama',
'rating':8.59
},
]

This file consists of the data of TV Series which is a list of dictionaries.

higher_lower.py

First, the required libraries, i.e., random, os, data and the ASCII art are imported. After initializing all the functions, we define clear() to clear the console when needed and call the play_game() function to start the code.

#import required modules
from art import logo, vs
from random import choice
from data import tv_series
from os import system

#######################
# print_tv_series(series, print_rating)
# answer(a,b)
# play_game()
#######################

The print_tv_series() function takes a dictionary of a TV series and a boolean of printing rating and prints the details of the TV series along with printing the rating only if the boolean value is True.

def print_tv_series(series, print_rating):
"""This function takes dictionary of a series and boolean of printing rating and print the details of tv series along with printing rating only 'TV Series A'."""
print(f"""
Name: {series['name']}, {series['year']}
Genre: {series['genre']}""")
if print_rating:
print(f"Rating: {series['rating']}")

The answer() function takes two TV series as input and returns the name of the TV series which has a higher rating.

def answer(a,b):
"""returns the name of TV Series which have higher rating"""
return a if a['rating'] >= b['rating'] else b

The play_game() function is the main function that is responsible for running the game. It first initializes the score to 0 and sets the user_play variable to True.

def play_game():
"""this is the main function of playing the game"""
#define score
score=0

#define the boolean of user_play
user_play = True

It then starts a while loop, which runs until the variable user_play is set to False. Using the clear() and print() functions, it clears the console and prints the logo. If the user has previously played a round, the function publishes the current score; otherwise, it selects a TV series A at random from the tv_series list of dictionaries.

The function then prints the details of TV series ‘A’ using the print_tv_series() function, followed by the “vs” ASCII art with the print() function. The code then picks a TV series ‘B’ from the tv_series list and uses the print_tv_series() function to print its data.

    while user_play:
#clear the console first
clear()

#prints logo to screen
print(logo)

#if the score is greater than 0. Prints a message to the console to show score. else randomly choose tv series A at first time
if score > 0:
print(f"Correct Answer. Your current score is: {score}")
else:
tv_series_a = choice(tv_series)

#print details of tv series A
print("TV Series A -")
print_tv_series(tv_series_a, True)

#print the ascii of vs
print(vs)

#randomly choose tv series B
tv_series_b=choice(tv_series)

#print details of tv series B
print("TV Series B -")
print_tv_series(tv_series_b, False)

The code then calls the answer() function to find out which TV series has a better IMDb rating, and the right answer is stored in the correct_answer variable. It then uses the input() function to prompt the user to pick either TV series ‘A’ or ‘B’ and keeps their response in the user_choice variable.

        #compare ratings by calling answer function and have the correct tv series name as an answer
correct_answer = answer(tv_series_a, tv_series_b)

#ask user to choose a movie by typing A or B
user_choice = input("\nWhich TV Series Have Higher IMDb Rating?\nType 'A' or 'B' - ").lower()

If the user chooses the right TV series (A or B), the score is increased by one, but if they choose the wrong one, the function displays a “Wrong Answer” stating the final score. It then asks the user if they want to continue playing, and if they say “N,” it sets user_play to False and exits the while loop, thereby finishing the game. If they choose “Y,” the score is reset to zero and the game restarts with a new round.

        #if correct A: then score will increase by 1
#if correct B: then score will increase by 1 and tv series B will be the tv series A in next round
#if failed: print wrong answer message and show the final score
if user_choice=='a' and correct_answer == tv_series_a:
score+=1
elif user_choice=='b' and correct_answer == tv_series_b:
tv_series_a = tv_series_b
score+=1
else:
print(f'Wrong Answer. Your final score is - {score}')

#ask user to play again
user_play_game = input("Do you want to play again? Type 'Y' or 'N' - ").lower()
if user_play_game == 'n':
user_play = False
print("\nThank you for using the program. Goodbye!")
else:
#reset the score
score = 0

Full Code

Sample Run

Sample run of Higher Lower Game in Python
Sample Run of Higher Lower Game

Features

Program’s Features

The program’s features include:

  • Randomly generated TV series: The program randomly generates two TV series in first round of the game. And continuously generates TV Series B randomly throughout the game.
  • User input: The program prompts the user to guess which of the two series has the higher rating on IMDb.
  • Score tracking: The program tracks the user’s score and displays it on the console after each round.
  • Replay ability: The program prompts the user to play again after losing a round.

Python Concepts and Techniques Used

The Python concepts and techniques used in the program include:

  • Functions: The program uses several functions to perform specific tasks, making the code more modular and easier to read and understand.
  • Loops: The program uses a while loop to allow the user to play the game multiple times.
  • Conditional statements: The program uses if-elif-else statements to check the user's input and update the score accordingly.
  • Random module: The program uses the random module to generate random TV series for each round.

Finally, this Python application is a fun way to test your knowledge of the most popular TV shows on IMDb. You’ll learn about functions, loops, conditional statements, and the random module by playing the game. So, what are you looking out for? Try the program out and see how many points you can get!

Ready to Level Up? Complete These Challenges

Now that you’ve learned how to create a higher-lower game, it’s time to put your skills to the test! Here are some fun challenges to help you take your Python knowledge to the next level. Are you ready to level up? Let’s go!

  1. Modify the print_tv_series() function to include the series' IMDb link as well.
  2. Add a feature to the game that allows the user to select the genre/year range of TV series they want to play with.
  3. Rewrite the play_game() function to use recursion instead of a while loop.

This article is a part of my Python Project Journal. To learn more about my journey in learning Python and to see my other projects, check out this project journal. Thank you!

--

--