Secondly, note that we use "=" to assign a value to a variable, so we can't using for asking if two things are equal. Instead, in conditions, we use double-equals "==". It's a classic error to do this:
if decision == "N":
print("This line done if condition true.")
print("This line always done.")
Thirdly, note that we can check for more than one thing at once using the Boolean operators
"and
" and "or
", which work like they do in English, so:
if decision == "N" or decision == "n" :
print("This line done if either condition true.")
if decision == "N" and name == "Alice" :
print("This line done if the user has typed N and their name is Alice.")
Last, note that at the moment if the game ends, the code continues on, saying the way is lit by candles. If we put this inside the if-block that continues the game, we won't have this problem.
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.")
Can you write a better version of our current if-statement, which takes into account that users may type lowercase letters and letters that are neither Y or N?
[Home > Part 1 > Next]