First update with working code
This commit is contained in:
45
ghost.py
Normal file
45
ghost.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from random import randint
|
||||
|
||||
# Keep variable initialisation away from any statements that 'do' something, like printing
|
||||
score = 0
|
||||
|
||||
# This is a function to help prevent code duplication
|
||||
# We pass in the score variable in brackets so that the function can use it
|
||||
def print_score(score):
|
||||
# Here we use an 'f-string', note the f before the quotes
|
||||
# That lets us use variables inside curly brackets
|
||||
print(f"Your score is: {score}")
|
||||
|
||||
|
||||
|
||||
# We don't need to track this as we will loop until we break
|
||||
while True:
|
||||
|
||||
# Call our custom function to print the scor
|
||||
print_score(score)
|
||||
|
||||
# This is one way to do a multi-line string
|
||||
print("""Three doors ahead...
|
||||
A ghost behind one.
|
||||
Which door do you open?""")
|
||||
|
||||
# We don't need to store the input or ghost door so can compute them at the time of use
|
||||
# We test the negative (!=), just to flip the order for aesthetics
|
||||
if int(input('Choose door 1, 2, or 3: ')) != randint(1,3):
|
||||
|
||||
# An alternative way to print more than one line, with \n
|
||||
print ('No ghost!\n You enter the next room')
|
||||
|
||||
# This is another way to increment a variable
|
||||
score += 1
|
||||
|
||||
# Else in this case means that the input WAS the same as the random int
|
||||
else:
|
||||
# this will break us out of the while True loop
|
||||
break
|
||||
|
||||
# If we are here then we've lost
|
||||
print ('GHOST!\nRun away!\nGame Over')
|
||||
print_score(score)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user