Goblin Hunt >>> Lesson 3: Finding the goblin



The fun starts now. So far we have welcomed the player to our game and set up the kitchen cupboards. In doing so we have used two data types: strings and integers, and we have chosen variable names to store this data in our program.

Our next job is to hide the goblin in one of the kitchen cupboards, and then ask the player to guess where the goblin is hiding. We currently have 5 kitchen cupboards. Let us, for now, hide the goblin in the third cupboard. Add the following line to the code you already have (best to add a blank line before it too):

goblin_position = 3

We have created a new variable that we chose to name goblin_position and assigned it the value of 3 (an integer). In the last lesson in this series we will change this to a random number since if the goblin is always hiding in the same place it wouldn't be much fun to play the game!

Next let's ask the player to guess where the goblin is hiding. We can re-use the input() function we have used earlier in our code.

chosen_position = input("Where is the goblin hiding? [Enter cupboard number] -> ")

chosen_position = int(chosen_position)

The response of the player is stored in a variable we named chosen_position in the first of these two lines. The input() function however gives back a string. This means that if the player types in 3, say, the input() function returns the character "3" rather than the number 3. To a human being like you and I, it is fairly obvious that these are the same thing, but not to a computer. We therefore have to change the character "3" to a number, or more precisely an integer 3 using the int() function. We won't dwell on this point too much in this brief course. Don't worry if this is not perfectly clear.

We have now reached a point in our program where we need to make a decision. What happens next depends on whether the player guessed correctly or not. This is how we deal with this:

if chosen_position == goblin_position:

print("Well done, you win")

A few things to note: there are two = signs, there's a colon (:) at the end of the first line, and there's an indent in the second line (pythonanywhere.com will do this automatically for you.) We will discuss each of these briefly. The first line is an if statement and reads out as follows: 'if chosen_position is equal to goblin_position'. If this is true the line that comes after the if statement is run. If it is false, however, the second line will be ignored. Try running the program a few times using both the correct value (currently 3) and incorrect ones. Can you figure out what is happening?

The colon (:) tells the computer program that what comes next should only be run if the statement is true, and the indent shows which lines belong to this if statement. There's only one line at the moment but there can be more (as we will see in the next lesson). And whereas we used a single = to store some data in a variable, we use == to ask the question: 'are these two variable the same?'

What about when the player makes an incorrect guess? We can expand the if statement above as follows:

if chosen_position == goblin_position:

print("Well done, you win")

else:

print("Sorry. You lose! Better luck next time.")

If the if statement is not true, then the line of code after the else command will be run.

We will build on this decision making process in Lesson 4. How is it going so far? Any questions, please email: info@codetoday.co.uk

#ifstatements #coding #ifelse

Recent Posts

See All
  • Facebook
  • Twitter
  • Instagram

© Codetoday Limited

Codetoday Limited is a company registered in England (company number 9789836).

Registered office: 13 Hawley Crescent, London, NW1 8NP, United Kingdom

Terms & Conditions

Privacy policy

Cookies policy