Starship Takeoff

You are a starship captain. You have crashed your ship on a strange planet and must take off again quickly in the alien ship you have captured. The ship’s computer tells you the gravity on the planet. You must guess the force required for a successful take off. If you guess too low, the ship will not lift off the ground. If you guess too high, the ship’s fail-safe mechanism comes into operation to prevent it from being burnt up. If you are still on the planet after ten tries, the aliens will capture you.

The code

# Import the 'random' module, so you can generate random numbers
import random


print('Starship Take-off')
print('-----------------')
print()

# Select 2 random numbers. `random.randrange(X)` picks a number
# between 0 and X, *including* 0, but *not* including X. Adding
# 1 means we get a number between 1 and X, *inclusive*.
gravity = random.randrange(20) + 1
weight = random.randrange(40) + 1

# Compute the required launch force.
required = gravity * weight

# Print the gravity for the planet
print('Gravity =', gravity)

# Start a loop that will run until either there have been
# 10 attempts, or the user entered the correct force.
success = False
c = 0
while c < 10 and not success:
    # On each loop, increment the count of attempts,
    # and prompt the user for a value
    c = c + 1
    force = int(input("Force:"))

    # Compare the value entered with the required value.
    # If they match, set the success flag; otherwise,
    # print an error
    if force > required:
        print('Too high!')
    elif force < required:
        print('Too low!')
    else:
        success = True

    # As long as this isn't the last loop,
    # we can try again
    if c < 10:
        print('Try again.')

print()

# We've either been successful, or we've done 10 loops.
# Tell the user if they've been successful.
if success:
    print('Good take off!!')
else:
    print('You failed - the aliens got you')

How to make the game harder

You can change to the program to give you less than 10 goes. Do this by altering the number on lines 24 and 42 (They must be the same).

Puzzle corner

You could change the range of possible forces. See if you can work out how.