Cards Against Humanities
A programming challenge for students who don't like History, Geography, MFL or R.E.
How the game works
Cards Against Humanities is a phrasal template word game, where one player prompts other players for a list of words to substitute blanks in a question or phrase before reading out the completed phrase or question aloud.
Example:
We start with a question or phrase containing one or more blanked out words. Here’s a basic example:
Rumble in the _______.
We then prompt the user to fill in the gaps, in this case, with a noun or place. We might end up with something like this:
Rumble in the Toilet.
Cards Against Humanities is a phrasal template word game, where one player prompts other players for a list of words to substitute blanks in a question or phrase before reading out the completed phrase or question aloud.
Example:
We start with a question or phrase containing one or more blanked out words. Here’s a basic example:
Rumble in the _______.
We then prompt the user to fill in the gaps, in this case, with a noun or place. We might end up with something like this:
Rumble in the Toilet.
Introduction
In this tutorial, you will learn how to code a “Cards Against Humanities” game in Python. The game will work by prompting the user to enter some words (e.g. person’s name, noun, adjective, place, object etc.) and substitute these with blanks in a quote from History, phrase in French or Spanish, or Geography question etc.
Examples
In this tutorial, you will learn how to code a “Cards Against Humanities” game in Python. The game will work by prompting the user to enter some words (e.g. person’s name, noun, adjective, place, object etc.) and substitute these with blanks in a quote from History, phrase in French or Spanish, or Geography question etc.
Examples
Activity 1 – Creating your first Cards Against Humanities game
There are several ways to create a Cards Against Humanities game in Python. In our version, the program will pick a phrase or question at random from a list of humanities subjects (History, Geography, Moder Foreign Languages or Religous Studies). However, before we start coding our “Ultimate Cards Against Humanities” game, let’s start with a simple version:
Step 1. Creating your first Cards Against Humanities game
Type in and run the following code:
There are several ways to create a Cards Against Humanities game in Python. In our version, the program will pick a phrase or question at random from a list of humanities subjects (History, Geography, Moder Foreign Languages or Religous Studies). However, before we start coding our “Ultimate Cards Against Humanities” game, let’s start with a simple version:
Step 1. Creating your first Cards Against Humanities game
Type in and run the following code:
noun = input("Enter a noun: ")
print("Rumble in the " + noun) |
Use the code window (above) to test your code.
Challenge:
Example:
- Create a Cards Against Humanities card that contains 2 or more user inputs (i.e. a phrase or questions that contains 2 or more blanks).
Example:
verb = input("Enter a verb: ")
verb2 = input("Enter another verb: ") plural = input("Enter a plural noun e.g. Cats or Dogs: ") print("That's one small " + verb + " for man, one giant " + verb2 + " for " + plural.") |
Activity 2 – Cards Against (Version 2)
In the next example, you are going to create a text file containing several questions or phrases (containing blanks). Just like the previous example, the program will ask the user to enter one or several nouns, verbs, adjectives etc. but this time will pick a question or phrase at random from a text file.
Step 1. Introducing the ‘String replace method’
Before you create your text file, let's first explore the ‘string replace method’.
Sometimes in Python, you find yourself in a situation where you want to modify the contents of string by replacing one piece of text with another. Luckily, Python makes this job easy thanks to the string replace method.
1. To understand how the .replace() method works, try the following code:
In the next example, you are going to create a text file containing several questions or phrases (containing blanks). Just like the previous example, the program will ask the user to enter one or several nouns, verbs, adjectives etc. but this time will pick a question or phrase at random from a text file.
Step 1. Introducing the ‘String replace method’
Before you create your text file, let's first explore the ‘string replace method’.
Sometimes in Python, you find yourself in a situation where you want to modify the contents of string by replacing one piece of text with another. Luckily, Python makes this job easy thanks to the string replace method.
1. To understand how the .replace() method works, try the following code:
string = "This is a string"
string = string.replace("is", "was") print(string) |
Use the code window (above) to test your code.
In the above example, the line ‘string = string.replace(“is”, “was”) looks for the word “is” in the sentence and replaces it with the word “was”. We will be using the .replace() method to replace the blank(s) in our Cards Against Humanities game with the word(s) input by the user.
Step 2. Creating the text file
Next, we need to create a text file for our Cards Against Humanities game, for the purpose of this example, we will call the file 'cards.txt'.
Challenge
Create a list of historical quotes, geography questions or phrases in French/Spanish etc. and store each one on a different line – Type in the word ‘blank’ for the word you want to be replaced, for example:
Step 2. Creating the text file
Next, we need to create a text file for our Cards Against Humanities game, for the purpose of this example, we will call the file 'cards.txt'.
Challenge
Create a list of historical quotes, geography questions or phrases in French/Spanish etc. and store each one on a different line – Type in the word ‘blank’ for the word you want to be replaced, for example:
MFL: Je voudrais un ananas is French for I would like a blank
History: In the final battle at Hastings, King Harold was killed by a blank in the eye Geography: Plucking is a process of erosion that occurs during blank |
Use the code window (above) to test your code.
Step 3. Writing the code
1. Enter the following code (make sure that you read the comments explaining what the code is doing):
1. Enter the following code (make sure that you read the comments explaining what the code is doing):
# Program to read a random question or phrase from a file and print the game card with the user's response
import random # Open the Cards Against text file f = open('cards.txt','r') # Read the whole file and store each line in a list cardText = f.readlines() # Choose a random line from the list card = random.choice(cardText) # Ask the user to input a noun noun = input("Enter a noun: ") # Replace the blank with the user's input card = card.replace("blank", noun) # Print out the game card including the user's response print(card) |
Challenge:
- modify your code so that it displays the randomly chosen card before prompting the user for their response.
Resources:
Cards Against Humanities Solution
|
|
Taking it further
Run the sample code below, making sure that you read the comments so that you understand how the code works. Tinker with the code / customise the text files to make the program your own.
Solution (Advanced)
|
|
Keywords:
Python web, python programming language, python programming, python coding.
Python web, python programming language, python programming, python coding.