Skip to content

Instantly share code, notes, and snippets.

@askingalot
Created March 30, 2022 19:11
Show Gist options
  • Save askingalot/7f02c3f21e949318e85b086faa53a324 to your computer and use it in GitHub Desktop.
Save askingalot/7f02c3f21e949318e85b086faa53a324 to your computer and use it in GitHub Desktop.
Extra Clothesline Challenges

Extra Clothesline Challenges

Challenge One - Difficulty Levels

Update your program so that when it starts the user is prompted to enter a difficulty level. The difficulty level they choose should affect the gameplay as described below.

The available difficulty levels should be:

Level Description
Cheater This level lets the user guess forever. An incorrect guess should not remove a clothespin.
Easy This level gives the user four free guesses before any clothespin is removed.
Normal This level works the same way the game did before this feature. Users should get eight incorrect guesses.
Hard This level gives the user only four incorrect guesses.
Ridiculous This level gives the user only one incorrect guess.

The difficulty level should be displayed above the clothesline image. It should continue to be displayed throughout the game.

If the user does not select a difficulty level, the game should pick one at random.

Challenge Two - Command Line Arguments

A command line argument is some kind of text that's added to the end of a command to alter the command's behavior.

Let's explore this with an example. Imagine you're using your terminal and you'd like to see the names of all the Python files in your workspace directory and ignore any other files and directories that are in the workspace directory.

cd ~/workspace
ls *.py

Go ahead and give it a try in your terminal.

Notice the *.py command line argument we're passing to the ls command. This tells ls to only list files that end in .py.

Reading Command Line Arguments in a Python Program

We can read command line arguments in our Python programs using the sys.argv list. Take a look at the following example to see it in action:

print_args.py

import sys

print()
print("Here are the arguments:")
print(sys.argv)

print()
print("Here's the firist 'real' argument:")
print(sys.argv[1])

Here's what we'd see if we ran the print_args.py program:

$ python3 print_args.py good times

Here are the arguments:
['print_args.py', 'good', 'times']

Here's the firist 'real' argument:
good

Here are a few things you should notice about the example:

  1. In order to use sys.argv we must import sys at the top of the file.
  2. sys.argv is a normal Python list.
  3. The first element (i.e. sys.argv[0]) of the sys.argv list is the name of our Python program.

The Challenge

Your challenge is to update your Clothesline code to accept the name of the word file as a command line argument. This means if the user wants to play with the fruits.txt file they should be able to run your program like this:

$ python3 clothesline.py fruits.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment