Dark theme

Hopefully you got that working - if not, check the punctuation and indenting carefully.

When you've got this working, let's test it a little harder. Run the code, but try typing "n" (lowercase) and "G". How does it respond?

We'll come back to these issues. First, let's look at the code in more detail. The code we've added uses an if-statement to make a decision.

decision = input("Do you want to enter? [Y,N]: ")
if decision == "N":
    print("Your quest is at an end, goodbye!")
else:
    print("You enter the mines.")
print("The way is lit by candles on the wall.")

The "if" part is made up of the keyword "if" (keywords are special words built into the language), then a condition, i.e. something to check, and then finally a colon ":". The colon marks the start of a block of code that will happen if the condition is true; in our case, if the variable "decision" is equal to "N". This is the first indented block.

The "else" is the same, but it's block runs if the "if" condition isn't true. After the else-block we dedent the code, and the line
print("The way is lit by candles on the wall.")
runs whether the condition is true or not.

There's a few things to point out about if-statements which will be of use when we think about the problems we found at the start of this page...

[Home > Part 1 > Next]