Open advent7 and File, Save As, advent8
This command is useful to exit a program before it reaches the bottom of the script.
Vocabulary: sub-routine is another name for a simple procedure or function without parameters. A simple section of code that could be called many times.
import sys #Import sys at the top of your program
if hitpoints <= 0: #If hitpoints are equal to or less than 0
print("Your hero is dead") #Final message before you end the program
sys.exit(0) #Command that exits the program
This could also be written as a function that could be checked after each decision.
Sub Routines, Procedures and Functions are great ways to organise your code. You can create Sub Routines, Procedures and Functions and run them as often as you want anywhere in your code.
import sys
exp = 0
hp = 1
def check_hp(): #This function checks the amount of hitpoints and ends the game if it reaches 0
if hp <= 0:
print("Your hero is dead")
sys.exit(0)
else:
print("You have", hp, "hit points left")
event1 = input("Type s to sneak by, type a to attack")
if event1 == ("s"):
print("Carefully you edge past")
exp=exp+2
else:
print("You throw a stone at the rat")
print("It attacks you")
print("After a struggle it runs off")
exp=exp+1
hp=hp-1
check_hp() #This command runs the function created above in bold
See if you can use the sys command to end the adventure if the hit points reduce to 0 or below 0.