Level Up

Every win makes the hero stronger. In role-playing games, heroes collect experience points, or XP, and with enough of them, they reach the next level. Three new variables hold the numbers: heroXp is how much XP the hero has before the fight, xpPerLevel how much XP makes one level, and dragonXp how much XP the hero gets for defeating the dragon. A hero who loses gets nothing.

The hero starts at level 1 and goes up one level for every 500 XP: with 0 to 499 XP, the hero is at level 1, with 500 to 999 XP at level 2, and so on. Before the fight, the hero has 2350 XP, which is level 5. The win brings 400 XP, 2750 in total, and that's already level 6.

At the end, the program prints two more lines: the hero's XP after the fight and the hero's level. When the fight has brought the hero to a new level, (level up!) follows the level. For our hero, the output ends like this:

The hero wins. Health left: 16
XP: 2750
Level: 6 (level up!)

The exercise checks other fights too: with heroXp = 2000, the hero ends with 2400 XP, still at level 5, so the last line is just Level: 5. And without armor, the hero loses, keeps 2350 XP and stays at level 5 too.

That's the whole project. With just three lessons behind you, you've written the heart of a game: it calculates with variables, makes decisions with conditions and prints a clear report of the fight. In the next lesson, you'll learn about vectors, which hold many values in one variable, such as the health of every monster in a dungeon.

Instructions

Print how much XP the hero has after the fight.

Below that, print the hero's level. When the fight has brought the hero to a new level, add (level up!), as in the example.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

3/3