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.
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
.
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:
- In order to use
sys.argv
we mustimport sys
at the top of the file. sys.argv
is a normal Pythonlist
.- The first element (i.e.
sys.argv[0]
) of thesys.argv
list is the name of our Python program.
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