Adventure Variables (AD4)
Open advent4 and File, Save As, advent5
A fantasy adventure would have hit points and experience points. You would start the adventure with a number of hit points and lose one every time you make a poor decision. Every time you survived an encounter or made a good choice, you would gain experience. How to Boss school might have detention points instead of hit points and learning points instead of experience points.
What variable names will you choose?
Vocabulary: integers are positive and negative whole numbers without fractions.
In the example below, you will see that both variables, exp and hp, were declared at the top of the program. This is called initialising a variable. The only thing that will go above initialising variables would be any imports you made, such as
from time import sleep
which we added when creating pauses in the last section.
exp = 0 #you start with no experience
hp = 10 #you start with 10 hitpoints
event1 = input("Type s to sneak by, type a to attack")
if event1 == ("s"):
print("Carefully you edge past")
exp=exp+2 #Two points are added to the experience variable
else:
print("You throw a stone at the rat")
print("It attacks you")
print("After a struggle it runs off")
exp=exp+1 #One point is added to the experience variable
hp=hp-1 #One point is subtracted from the hitpoints variable called hp
Inside your if and else choice selections add variable to increase or decrease your experience and hit points.
At the end of a choice it is a good idea to let the player know how they are getting on.
We can do this by using the hp and exp type variables in print commands.
For example
print("You have",exp,"experience points so far")
print("You have",hp,"hit points left")
NOTE that the text has speech marks around it, but the variables don't, and that text and variables are separated by commas. When you print these, none of the separator commas appear on the screen. If you want a comma to be used as a comma, make sure it is inside the speech marks.
If you want everyone to view the points, these print statements should follow the if-and-else choices and not be indented.
Run your code and test it.
If you have errors, DEBUG them.
Add at least two variables to your existing code and modify the values depending on the user's decision.