This tutorial will show how to create a simple number-guessing game, in both Python and Scratch, and compare the two methods.
Scratch
The full game is as follows:
when gf clicked set [tries v] to [6] set [limDn v] to [1] set [limUp v] to [100] set [secret v] to (pick random (limDn) to (limUp)) say [Arr! I'm the Dread Pirate Roberts, and I have a secret!] for (2) seconds say [It's a number from 1 to 100. I'll give you 6 tries.] for (2) seconds repeat until <<(answer) = (secret)> or <not <(tries) > [0]>>> ask [What's yer guess, matey?] and wait if <<(answer) < (limDn)> or <(answer) > (limUp)>> then say [Out of range, ye swab!] for (1) seconds else if <(answer) > (secret)> then say [Too high, landlubber!] for (1) seconds set [limUp v] to (answer) end if <(answer) < (secret)> then say [Too low, ye scurvy dog!] for (1) seconds set [limDn v] to (answer) end end say (join (join (join (join [Yer range is ] (limDn)) [ to ]) (join (limUp) [. Ye have ])) (join (tries) [ tries left.]) change [tries v] by (-1) end if <(answer) = (secret)> then say (join [Avast! Ye got it! Found my secret, ye did! With ] (join (tries) [ tries left!])) else say (join [No more tries, matey! Better luck next time! The secret number was ] (secret)) end
Put the script above in the default sprite.
Python
import random
tries = 6
limDn = 1
limUp = 100
secret = random.randint(limDn, limUp)
guess = 0
print("Arr! I'm the Dread Pirate Roberts, and I have a secret!")
print("It's a number from 1 to 100. I'll give you 6 tries.")
while guess != secret and tries > 0:
guess = int(input("What's yer guess, matey? "))
if guess < limDn or guess > limUp:
print('Out of range, ye swab!')
continue
if guess > secret:
print('Too high, landlubber!')
limUp = guess
elif guess < secret:
print('Too low, ye scurvy dog!')
limDn = guess
print('Yer range is', limDn, 'to', str(limUp) + '. Ye have', tries, 'tries left.')
tries -= 1
if guess == secret:
print('Avast! Ye got it! Found my secret, ye did! With', tries, 'tries left!')
else:
print('No more tries, matey! Better luck next time! The secret number was', secret)
Step-By-Step Comparison
Initialization and Introduction
Here is how both scripts start off:
- Set a bunch of variables that will be used later on:
tries
: The number of attempts left to guess the secret.limDn
: The last guess that was less than the secret. This is 1 at the start, since no guess should be less than 1.limUp
: The last guess that was more than the secret. This is 100 at the start, since no guess should be more than 100.secret
: The number that the player is guessing. This is a random number betweenlimDn
andlimUp
(i.e. 1 and 100).guess
: The variable holding the player's last guess. In Scratch, this is simplified to just using the(answer)
block.
- Output the introduction to the game to the player. The introduction states, "Arr! I'm the Dread Pirate Roberts, and I have a secret! It's a number from 1 to 100. I'll give you 6 tries."
Scratch
set [tries v] to [6] set [limDn v] to [1] set [limUp v] to [100] set [secret v] to (pick random (limDn) to (limUp)) say [Arr! I'm the Dread Pirate Roberts, and I have a secret!] for (2) seconds say [It's a number from 1 to 100. I'll give you 6 tries.] for (2) seconds
The Scratch script sets the variables using the set [ v] to []
block. For the (secret)
variable, the (pick random () to ())
block picks a random number from (limDn)
to (limUp)
, i.e. 1 to 100.[1]
After setting the variables, it uses the say [] for () seconds
block to show the introduction to the player.[2]
Python
import random
tries = 6
limDn = 1
limUp = 100
secret = random.randint(limDn, limUp)
guess = 0
print("Arr! I'm the Dread Pirate Roberts, and I have a secret!")
print("It's a number from 1 to 100. I'll give you 6 tries.")
Before setting the variables, the Python script needs to import a module called "random", which provides a number of different functions to do with randomness. Once it's imported, each variable is set using the variable = value
syntax. For the secret
variable, the script uses the randint
function provided by the "random" module to generate a random number between limDn
and limUp
.
Additionally, since Python has no equivalent to Scratch's (answer)
block, the guess
variable needs to be set. It is set to 0, so that it cannot possibly equal the secret at the start.[1]
After setting the variables, the script uses Python's built-in print
function to output the introduction. Using the function twice results in two separate lines of output, which is desired here.[2]
Main Loop
After initializing the variables and showing the introduction, it is time to enter the main loop of the game. The process is:
- Check whether the last guess was the same as the secret, or the number of tries is no longer greater than 0. If it was, don't start another loop sequence - just finish here.
- Ask for the player's guess, using the prompt "What's yer guess, matey?" and set the
guess
variable to that guess. - If the guess is out of the
limDn
-limUp
range:- say so, with the message "Out of range, ye swab!" and
- skip to the end of the loop.
- Otherwise, if the guess is more than the secret:
- say so, with the message "Too high, landlubber!", and
- set the
limUp
variable to the guess.
- Otherwise, if the guess is less than the secret:
- say so, with the message "Too low, ye scurvy dog!", and
- set the
limDn
variable to the guess.
- Show the current
limDn
-limUp
range, and the number oftries
left. - Count down the number of tries by 1.
- Start from the beginning of the loop again.
Scratch
repeat until <<(answer) = (secret)> or <not <(tries) > [0]>>> ask [What's yer guess, matey?] and wait if <<(answer) < (limDn)> or <(answer) > (limUp)>> then say [Out of range, ye swab!] for (1) seconds else if <(answer) > (secret)> then say [Too high, landlubber!] for (1) seconds set [limUp v] to (answer) end if <(answer) < (secret)> then say [Too low, ye scurvy dog!] for (1) seconds set [limDn v] to (answer) end end say (join (join (join (join [Yer range is ] (limDn)) [ to ]) (join (limUp) [. Ye have ])) (join (tries) [ tries left.]) change [tries v] by (-1) end
A loop in a Scratch script is done using any of the "repeat" blocks or the "forever" block. Here, since a condition needs to be checked every loop, the script uses the repeat until <>
block.
The "repeat until" block stops looping when the condition is true. Therefore, the condition inserted needs to check whether the guess is the same as the secret, or the number of tries is no longer more than 0. In the script, <(answer) = (secret)>
checks whether the guess is the same as the secret, the first of the two conditions. <not <(tries) > [0]>>
checks whether the number of tries is no longer greater than 0, the second of the conditions. When each of those conditions is inserted into a <<> or <>>
block, the condition becomes when either of those sub-conditions is true.[1]
The ask [] and wait
block brings up a prompt asking for the guess. After the player has entered their guess, (answer)
will then be updated to report that guess.[2]
if <> then else end
block to check whether the guess is out of range. The condition used checks whether the guess is less than (limDn)
or more than (limUp)
. If it is, it uses the "say" block to display the "out of range" message.[3.1]
The "else" part of the block wraps the blocks for the next two steps, so that if the condition is true it effectively skips almost to the end of the loop.[3.2]
In the "else" part, there is an additional if <> then
block. If the guess is more than (secret)
, it displays the message,[4.1] and sets (limUp)
to the guess.[4.2]
Also in the "else" part, the second "if" block checks whether the guess is less than (secret)
. If it is, it displays the message,[5.1] and sets (limDn)
to the guess.[5.2]
After all is done, the script uses a "say" block combined with a multitude of (join [] [])
blocks to output the current range, and the number of tries left.[6]
After reporting the status, the script uses the change [ v] by ()
block to reduce the number of tries by 1.[7]
Then the repeat block starts over from the top.[8]
Python
while guess != secret and tries > 0:
guess = int(input("What's yer guess, matey? "))
if guess < limDn or guess > limUp:
print('Out of range, ye swab!')
continue
if guess > secret:
print('Too high, landlubber!')
limUp = guess
elif guess < secret:
print('Too low, ye scurvy dog!')
limDn = guess
print('Yer range is', limDn, 'to', str(limUp) + '. Ye have', tries, 'tries left.')
tries -= 1
A loop in a Python program can be done using the "while" or "for" keywords. Since the point of this loop is to check a condition repeatedly, the "while" keyword is used.
The first line is the declaration of the loop. The "while" keyword starts a "while" loop. Following it is the condition to be checked every iteration: guess != secret and tries > 0
. A while block stops looping when the condition is false. Therefore, what needs to be checked is whether the guess is not the same as the secret, and the number of tries is still greater than 0. This condition accomplishes that check.[1]
Next, the guess
variable is set to int(input("What's yer guess, matey?"))
, which in short means to convert a line of user input into a number. This means that the guess
is now a number suitable for comparison.[2]
After that, there is an "if" block. The condition being checked is simply guess < limDn or guess > limUp
- meaning either the guess is less than limDn
or greater than limUp
. If the condition is true, print the "out of range" message,[3.1] and continue, which means to jump straight to the next iteration.[3.2]
Once the out-of-range check has passed, another "if" block arrives. If the guess is more than the secret, then print the "too high" message,[4.1] and set limUp
to the guess.[4.2]
Otherwise, if the guess is less than the secret, then print the "too low" message,[5.1] and set limDn
to the guess.[5.2]
Finally, print out the current status. In the print
function, it simply outputs all the arguments separated by spaces. Therefore the following line:
print('Yer range is', limDn, 'to', str(limUp) + '. Ye have', tries, 'tries left.')
will output "Yer range is", a space, the lower limit, another space, "to", space, the upper limit (converted to a string) concatenated with ". Ye have", another space, the number of tries left, and finally "tries left."[6]
After all of that, decrement tries
. In Python, var1 += var2
is a shortcut for var1 = var1 + var2
; same goes for many other operators, e.g. -, *, /, //, %, &, ^, |, @
.[7]
At the very end of the while loop, it loops back to the beginning and checks the conditions again, and the whole thing starts over.[8]
Finishing Off
After the main loop is over, there are two possible outcomes: winning or losing. The scripts' next task is:
- Check whether the last guess was the same as the secret. If so:
- Say so, including the number of tries. The message must be "Avast! Ye got it! Found my secret, ye did! With (tries) tries left!" where (tries) is the number of tries.
- Otherwise:
- Assume that the player has run out of tries, and reveal the secret when saying so. The message is as follows: "No more tries, matey! Better luck next time! The secret number was" and then the secret.
Scratch
if <(answer) = (secret)> then say (join [Avast! Ye got it! Found my secret, ye did! With ] (join (tries) [ tries left!])) else say (join [No more tries, matey! Better luck next time! The secret number was ] (secret)) end
Using the
if <> then else end
block, the logic is self-explanatory. If the <(answer) = (secret)>
,[1] then say the message, adding the number of tries left with a couple of join blocks.[1.1] Otherwise,[2] say the losing message, revealing the secret with another join block.[2.1]
Python
if guess == secret:
print('Avast! Ye got it! Found my secret, ye did! With', tries, 'tries left!')
else:
print('No more tries, matey! Better luck next time! The secret number was', secret
This is a simple if-else block. In Python, equality is determined using the ==
operator, so therefore the expression determining whether the last guess is the same as the secret is guess == secret
. If the guess equals the secret,[1] print the message, adding the number of tries using the multiple-argument functionality of the print function.[1.1] Otherwise,[2] print the losing message, revealing the secret with the second argument.[2.1]