Skip to content

Commit

Permalink
Added the flavour text and annotations to Robot Missile.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Dec 31, 2017
1 parent effc6a9 commit 97c2b95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,34 @@
Robot Missile
=============

The year is 2582 and the people of Earth are in the midst of a battle against
the Robots. A lethal Robot Missle has just landed and everyone is depending on
you to find the secret code which unlocks its defuse mechanism. If you fail,
the entire Earth Command Headquarters will be blown up.

Your computer knows what the code letter is. You must type in your guess and
it will tell you whether the code letter is earlier or later in the alphabet.
You have four chances to find the correct letter before the missile blows up.

The code
========

.. literalinclude:: robot_missile.py
:language: python

Adding to the program
---------------------

You can make the computer print an extra message for a correct guess on the
last go. Change line 37 to read::

print('You did it%s!' % (' (just)' if guesses == 4 else ''))

This inserts some text between "it" and the exclamation mark; the text that is
inserted is dependent on the value of `guesses` at the end of the loop.

Puzzle corner
-------------

See if you can work out how to change the program to give you more or less
changes of guessing the code letter.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Import `random` so we can generate random numbers.
# The `string` module gives us access to string-related utilities,
# like a list of lowercase characters.
import random
import string

Expand All @@ -7,26 +10,33 @@
print('You have 4 chances.')
print()

# Choose, at random, a lowercase ascii character
code = random.choice(string.ascii_lowercase)

# Loop until we get a match, or we've tried 4 times
guesses = 0
success = False

while not success and guesses < 4:
guess = input('What is your guess? ')
# Read a character from the user, and convert it to lower case
guess = input('What is your guess? ').lower()
guesses += 1

if guess.lower() == code:
# Check to see if the guess is correct. If it is, flag the success;
# otherwise, print an error message hinting towards the final answer.
if guess == code:
success = True
if guess < code:
elif guess < code:
print('The code is later than %s in the alphabet' % guess)
if guess > code:
elif guess > code:
print('The code is earlier than %s in the alphabet' % guess)

# The loop has exited. Let the user know if they succeeded or not.
if success:
print('TICK... FIZZ... CLICK...'
'\nYou did it!')
print('TICK... FIZZ... CLICK...')
print('You did it!')
else:
print('BOOOOOOOOMMM...'
'\nYou blew it!'
'\nThe correct code was \'%s\'.' % code)
print('BOOOOOOOOMMM...')
print('You blew it!')
print()
print("The correct code was '%s'." % code)

0 comments on commit 97c2b95

Please sign in to comment.