Teach Your Kids To Code: A Parent-friendly Guide to Python Programming
Random Fun and Games: Go Ahead, Take a Chance! 115 RandomSpirals.py import random import turtle t = turtle.Pen() turtle.bgcolor( ...
116 Chapter 6 to draw each line of the spiral. For m in range(size), the turtle will move forward a distance of m*2, drawing a l ...
Random Fun and Games: Go Ahead, Take a Chance! 117 RockPaperScissors.py u import random v choices = ["rock", "paper", "scissors" ...
118 Chapter 6 whether the player and the computer picked the same item; if so, we tell the user that the outcome was a tie. Othe ...
Random Fun and Games: Go Ahead, Take a Chance! 119 Pick a Card, Any Card One thing that makes card games fun is randomness. No t ...
120 Chapter 6 want to let the computer do the repetitive work. The suit names (diamonds, hearts, clubs, spades) are going to be ...
Random Fun and Games: Go Ahead, Take a Chance! 121 we have a random face and a random suit, all we do to complete a card name is ...
122 Chapter 6 For a visual representation of the suits array and the index of each suit, see Table 6-1. Table 6-1: The suits Arr ...
Random Fun and Games: Go Ahead, Take a Chance! 123 import random faces = ["two", "three", "four", "five", "six", "seven", "eight ...
124 Chapter 6 Because we start with keep_going = True, the program will enter the loop at least the first time. Next we’ll ask t ...
Random Fun and Games: Go Ahead, Take a Chance! 125 Here we add the while statement, so our loop will continue as long as keep_go ...
126 Chapter 6 Roll the Dice: Creating a Yahtzee-Style Game We used arrays in our card game to help simplify the code needed to d ...
Random Fun and Games: Go Ahead, Take a Chance! 127 of each of the rolled dice. Third, we’ll simulate the roll of the dice by ass ...
128 Chapter 6 For our test to see if the array of dice contains four of a kind, sorting the array means we only have to test for ...
Random Fun and Games: Go Ahead, Take a Chance! 129 looking at the value in the first dice element and comparing it to the value ...
130 Chapter 6 After we import the random module and start the game loop with a while statement, the next few lines deserve a lit ...
Random Fun and Games: Go Ahead, Take a Chance! 131 At y, we call the .sort() function on the dice array. This makes it easy to t ...
132 Chapter 6 Kaleidoscope The random spiral color graphic from Figure 6-2 looked a bit like a kaleidoscope. To make it look mor ...
Random Fun and Games: Go Ahead, Take a Chance! 133 To understand how to achieve this mirror effect, we need to talk more about C ...
134 Chapter 6 Let’s step through the full code for Kaleidoscope.py and see this in action. Kaleidoscope.py import random import ...
«
3
4
5
6
7
8
9
10
11
12
»
Free download pdf