Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1
Random Fun and Games: Go Ahead, Take a Chance! 109

If players learn to use this strategy, they can guess a number
between 1 and 10 in four tries or less, every time! Give it a shot!
To make the program more interesting, you could change the
arguments you pass to the randint() function to generate a number
between 1 and 100 or an even higher number (be sure to change
the input() prompts as well). You could also make a variable called
number_of_tries and add 1 to it every time the user guesses, to keep
track of the user’s number of tries. Print the number of tries at
the end of the program to let the user know how well they did. For
an additional challenge, you could add an outer loop that asks the
user if they want to play again after they guess the number cor-
rectly. Try these on your own, and go to http://www.nostarch.com/
teachkids/ for sample solutions.

Colorful Random Spirals


The random module has other handy functions besides randint(). Let’s
use them to help us create an interesting visual: a screen full of
spirals of random sizes and colors like the one in Figure 6-2.

Figure 6-2: Spirals of random sizes and colors at random locations
on the screen, from RandomSpirals.py
Free download pdf