Exercise 08-16:

Modify the alien game. Create a variable that represents the player’s life, starting with 100 points. The game ends when you find the alien or you run out of life (<=0). With each mistake, your life is decreased by a random value between 5 and 20 points, representing an attack by the alien. You can remove the part of the game’s code that limits the number of attempts and let only the player’s or alien’s life decide when the match ends. Show how much life the player has left before guessing the next tree number.

Answer:

##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 08/exercise-08-16.py.py
##############################################################################
import random

player_health = 100
tree = random.randint(1, 100)
print("An alien is hiding behind a tree")
print("Each tree has been numbered from 1 to 100.")
print("You have 3 attempts to guess which tree")
print("the alien is hiding behind.")

while player_health > 0:
    print(f"Health points: {player_health}")
    guess = int(input("Choose a tree [1-100]: "))
    if guess == tree:
        print("You got it right! The alien was found!")
        break
    elif guess > tree:
        print("Too high")
    else:
        print("Too low")
    damage = random.randint(5, 20)
    player_health -= damage

if player_health <= 0:
    print("You didn't survive. The alien won.")
    print(f"The alien was behind tree {tree}.")
Click here to download the file