Seminar 3 Guess the Number, the Sequel

During our previous seminar, you programmed a single-attempt-only “Guess the Number” game. Now, you will expand to multiple attempts and will add other bells-and-whistles to make it more fun. Download the exercise notebook before we start!

3.1 While loop (Exercises 1-2)

If you want to repeat something, you need to use loops. There are two types of loops: while loop, which is repeated while a condition is true, and for loop that iterates over items (we will use it later).

The basic structure of a while loop is

# statements before the loop

while <condition>:
    # statements inside are executed
    # repeatedly for as long as
    # the condition is True
    
# statements after the loop

The <condition> here is any expression that is evaluated to be either True or False, just like in an if-elif-else conditional statement.

Do exercise #1.

Let us use while loop, so that the player keeps guessing until finally getting it right. You can copy-paste the code you programmed during the last seminar or could redo it from scratch (I would strongly recommend you doing the latter!). The overall program structure should be the following

from random import randint

# generated random number and store in computer_pick variable
# print it out for debugging purposes
# get player input, convert it to an integer, and store

# while <players guess is not equal to the value the computer picked>:
    # print out "my number is smaller" or "my number is larger" using if-else statement
    
# print "Spot on!" (because if we got here that means guess is equal to the computer's pick)

Put your code into exercise #2.

3.2 Counting attempts (Exercise #3)

Now let us add a variable that will count the total number of attempts the player required. For this, create a new variable (call it attempts or something similar) before the loop and initialize it 0. Add 1 to it every time the player inputs the guess. After the loop, expand the "Spot on!" message you print out by adding information about the attempts count. Use string formatting to make things look nice, e.g. "Spot on, you needed 5 attempts".

Put your code into exercise #3.

3.3 Breaking (and exiting, Exercise #4)

While loop is continuously executed while the condition is True and, importantly, all code inside is executed before the condition is evaluated again. However, sometimes you may need to abort sooner without executing the remaining code. For this, Python gives you a break statement that causes the program to exit the loop immediately and to continue with the code after the loop.

# this code runs before the loop

while <somecondition>:
  # this code runs on every iteration
  
    if <someothercondition>:
        break
  
  # this code runs on every iteration but not when you break out of the loop

# this code runs after the loop

Do exercise #4 to build the intuition.

3.4 Limiting number of attempts via break (Exercise 5)

Let’s put the player under some pressure! Decide on maximal number of attempts allowed and stores in a constant. Pick an appropriate name (e.g. MAX_ATTEMPTS) and REMEMBER, ALL CAPITAL LETTERS for a constant name! Now, use break to quit the while loop, if current attempt number is greater than MAX_ATTEMPTS.

Put your code into exercise #5.

3.5 Correct end-of-game message (Exercise 6)

Think about the final message. Currently it says “Spot on…” because we assumed that you exited the loop because you gave the correct answer. With limited attempts that is not the case, as the player could out of the loop because

  1. They answered correctly
  2. They ran out of attempts.

Use if-else conditional statement to print out an appropriate message (e.g., "Better luck next time!, if the player lost).

Put your code into exercise #6.

3.6 Limiting number of attempts with a break (Exercise 7)

Modify your code to work without break statement. Modify your condition so that loop repeats while player’s guess is incorrect and the number of attempts is still less than the maximally allowed.

Put your code into exercise #7.

3.7 Show remaining attempts (Exercise 8)

Modify the input prompt message to include number of remaining attempts. E.g. "Please enter the guess, you have X attempts remaining".

Put your code into exercise #8.

3.8 Repeating the game (Exercise 9)

Let us an option for the player to play again. This means putting all the current code inside of another while loop that is repeated for as long as the player wants to play. The code should look following:

from random import randint

# define MAX_ATTEMPTS

# define a variable called "want_to_play" and set to True
while want_to_play:
  # your working game code goes here ()
  
  # ask user whether via input function. E.g. "Want to play again? Y/N"
  # want_to_play should be True if user input is equal to "Y"
  
# very final message, e.g. "Thank you for playing the game!"

Pay extra attention to indentation to group the code properly!

Put your code into exercise #9.

3.9 Best score (Exercise 10)

A “proper” game typically keeps the track of players’ performance. Let us record what was the fewest number of attempts that the player needed to guess the number. For this, create a new variable fewest_attempts and set it to MAX_ATTEMPTS (this is as bad as the player can be). Think, where do you need to create it? Once a game round is over and you know how many attempts the player required, update it if the number of attempts that the player used was less than the current value. You can add the information about “Best so far” into the game-round-over message.

Put your code into exercise #10.

3.10 Counting game rounds (Exercise 11)

Let us count how many rounds the player played. The idea and implementation is the same as with counting the attempts. Create a new variable, initialize it to 0, increment by 1 whenever a new round starts. Include the total number of rounds into the very final message, e.g. “Thank you for playing the game X times!”

Put your code into exercise #11.

3.11 Wrap up

Most excellent, you now have a proper working computer game with game rounds, limited attempts, best score, and what not!