0% found this document useful (0 votes)
39 views2 pages

Turtle Guess A Number

learning junior kids for scartch turtle

Uploaded by

M K Khaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views2 pages

Turtle Guess A Number

learning junior kids for scartch turtle

Uploaded by

M K Khaing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Learn More

Session 6

Session 6 Extension Activity: Guess a Number

Logic can be used to make fun games. Try it! Design a game where the player must pick a
number between one and ten. Can they guess it?

The code works something like this:

E
number=pick a random number guess=type number into input box

#check if the guess is right


if guess==number:
write("Yes! You are correct!")
else:

PL
write("No!")

1. Open IDLE (Python). Create a new file. Name it guess.

2. Import the Turtle library. Label the title bar and hide the Turtle symbol.

from turtle import *


title("Guess a Number")
hideturtle()
M
3. Import the Random library to pick a number:
from turtle import *
The Random library has a set
import random of commands that pick an
title("Guess a Number") unknown number or choice
hideturtle() from a list.

4. Pick a number between 1 and 10 and store it as a variable:


SA

#pick a number randint stands for random integer. An


integer is a number. This code picks a
number=random.randint(1, 10)
number between 1 and 10.

5. Test that the computer is picking a number:


a. From the File menu, select Save.
b. From the Run menu, select Run Module.
c. Close the canvas. It is blank.
d. In the Python Shell, type number. Press ENTER.

e. When done, close the Python Shell.

Copyright © TechnoKids Inc. 105 TechnoTurtle | Python


Session 6

6. Create an input box to store the player's guess as a variable:

#guess
guess=numinput("Guess", "Pick a number between 1 and 10.")

7. Run the program to test the input box.

E
8. Check to see if the player's guess is correct:

#check guess
if guess==number:
write("Yes! You are correct!")

PL
else:
write("No!")

9. Run the program to play the game. Did you guess the number?
TIP: Add font=("Arial", 20)
to make the text larger.
No!

write("No!", font=("Arial", 20))


M
10. If the player is wrong, tell them the number.

#check guess
if guess==number:
write("Yes! You are correct!", font=("Arial", 20))
else:
write("No! The number is " +str(number)+ ".")
SA

11. Run the program to play the game again. Did you guess the number?

TIPS:
• When testing the program make the range smaller such as random.randint(1, 2)

• Center the text using align="center"


• Format the color of the text using pencolor("green")
• Program the turtle to do an action if the player is correct:
penup()
goto(0, -200)
pendown()
circle(200)

12. Close the program.

Copyright © TechnoKids Inc. 106 TechnoTurtle | Python

You might also like