Escape!

The Robots have caught you, taken your weapons and locked you up. Suddenly you remember you still have your sonar wristwatch, which can be tuned to produre sounds of any frequency. If you can only find the resonant frequency of your Robot guards, they should vibrate so much they fall apart.

You must be careful not to use frequencies that are too low or the building will vibrate and collapse on top of you. If you go too high, you will get such a headache you will have to give up.

Can you escape the horrors of the Robot prison? (Look carefully at the program for a clue to the range of frequencies to try.)

The code

# Import:
# * random so we can generate random numbers,
# * sys to manipulate the terminal
import sys, random

# Includes some yorkshire4 terminal utilities, too
from yorkshire4.terminal import clear_screen


clear_screen()

# This chooses a number between 1 and 100 for the frequency of the Robots.
frequency = random.randint(1,100)

# These are used if you go too low or too high.
low = False
high = False

won = False
lost = False

# This is the beginning of a loop hich allows you to have 5 turns.
for turn in range(5):
    guess = int(input('What is your guess? '))

    # Checks if your guess is within 5 of the frequency.
    # If it is, it sets the won variable to True.
    if abs(frequency - guess) < 5:
        won = True
        break
    # Checks if your guess is so low it is less than the frequency by more than 40.
    # If it is, check that a low guess hasn't already been made, if it hasn't, print a warning.
    # If it isn't, the program tells you that you have lost.
    elif frequency - guess > 40:
        if not low:
            print('Too low... the building rumbles, careful...')
            low = True
        else:
            print('The building collapsed!')
            lost = True
            break
    # Checks if your guess is so high it is more than the frequency by more than 40.
    # If it is, check that a high guess hasn't already been made, if it hasn't, print a warning.
    # If it isn't, the program tells you that you have lost.
    elif guess - frequency > 40:
        if not high:
            print('Too high... ouch!')
            high = True
        else:
            print('Your head aches - you have to give up.')
            lost = True
            break
    else:
        print('No visible effect.')

# Checks what happened in the loop, if the game was won, print YOU'VE DONE IT.
# If not, check that there weren't too many low or high guesses before printing a message.
if won:
    print('YOU\'VE DONE IT!')
elif not lost:
    print('You took too long!')
    print('The frequency was %d.' % frequency)

Puzzle corner

The three Robrt guards each have their own resonant frequency. You can’t escape until you have found all three. How could you change the prograw to do this?

How to make the game harder

Change the number in the first “if” statement from 5 to a lower number. This means you have te get closer to the frequency to win. You can also increase the possible range of the frequency variable by changing the 100 to a higher number.