Advertisement

Friday, 5 May 2017

Just Enough Python 8 - a gui guessing game

We've actually done quite a lot in the previous tutorials. The last one introduced a GUI for our guessing game but it wasn't functional.
This time we are going to add some functionality so that it actually becomes a playable game, similar to the non-gui version we made earlier.

It still won't be perfect but closer to what we actually want (we'll produce a better version next time).


This version looks the same as the previous one but it works! We have properly defined the action that is invoked when we press the button. Here's the code...

# gui program using guizero with text fields and a button
# version 2 - it works, but doesn't stop

from guizero import App, Text, TextBox, PushButton
import random

# define the function makeAGuess
# this is called whe the guess button is pressed
def makeAGuess():
    guess = guessBox.get()
    response = Text(app, guess)
    if guess == answer:
        response.append(" : Right answer!")
    else:
        response.append(" : Wrong answer!")
     
    guessBox.set("")
     
     
# First get the number to guess
number = random.randint(1,10) # get a random integer, 1 to 10
answer = str(number)    # convert it to a string
     
# set up the app
app = App(title="Guessing game")

message = Text(app, text="Type a guess in the box below, a number from 1 to 10")

# A simple layout that positions a textBox
# where a guess will be entered...
guessBox = TextBox(app,"")

# adds a pushbutton to accept the guess...
button = PushButton(app, makeAGuess, text="Guess")

# and then puts another textBox to display the result
responseBox = TextBox(app,"")

app.display()
The first thing to notice is that we've imported the random package and included the code for generating a random number from the first version of the game.

import random 

and

# First get the number to guess
number = random.randint(1,10) # get a random integer, 1 to 10
answer = str(number)    # convert it to a string 

We do this before setting up the GUI and, as we have seen these before, no explanation should be necessary.

The main difference is this code where we properly define the function makeAGuess.

# define the function makeAGuess
# this is called whe the guess button is pressed
def makeAGuess():
    guess = guessBox.get()
    response = Text(app, guess)
    if guess == answer:
        response.append(" : Right answer!")
    else:
        response.append(" : Wrong answer!")
     
    guessBox.set("") 


We have replaced the pass statement (which does nothing) from our previous version with code to check whether the player's guess is correct.

The first thing we do is get the text from the text box guessBox. We do this by invoking the get() function that is a component of the text box like this...

guess = guessBox.get()

This means use the get() function in guessBox to return the string value in that box, i.e. whatever has been typed (if nothing has been typed the value returned will be the null string, "").

We set the variable guess to this value.

Next we create a new Text message and give it the value of guess, too (you will see why in a moment). Also assign it to the variable response. So if the player typed "5" as a guess then the value of the variable guess will be "5" and the new Text message will be displaying "5", too.

Now, we know that the correct answer is stored in the variable answer. So, we use an if statement to compare the values of guess and answer. If they are the same (i.e. the comparison is True) then the player has guessed right. And we use this result to append a message to our new Text message.

response.append(" : Right answer!")

Remember that the Text message already has the value of the guess. The function append() adds a string to the end of the text that is already there.

So if "5" is the correct answer then the message "5 : Right answer!" is displayed and "5 : Wrong answer!" is displayed otherwise.

The last line of the function is...

guessBox.set("")

This is executed whatever the outcome of the if statement and uses the set() function to set the content of guessBox to a null string (i.e. blank), ready for the next guess.

And that's it. The program will go on forever telling us whether the answer is right or wrong.

Now, wouldn't it be better if it stopped when we got the answer right? And then gave us the option of stopping or playing again?

Yes, I think so too, so we'll do that next time.

No comments:

Post a Comment