to open a Python language interpreter in a new tab or new window.
Then, as needed, paste in the text from each example into the main.py window of the interpreter, completely pasting over anything already in the window (including older examples).
Make small changes as needed to complete the worksheet.
Click the green ▶ Run button to execute the code. Results will appear near the bottom of the screen. If the code seems to be running for a very long time, you can use the ◼ Stop button to end the program immediately.
You can download the companion worksheet here.
Neil Simonetti
Python code to be pasted into the Python interpreter will show here when you click on the Show Example buttons.
# Example 1: Rolling a Die (ooh, exciting) import random dieRoll = random.randint(1,6) print("You rolled a",dieRoll) # end of Example 1
# Example 2: Rolling Lots of Dice (wait, you can do that?) import random numSides = 6 # change this to roll dice with a different number of sides numRolls = 20 # change this to change the number of rolls target = 0 # change this to change the target number countR = 0 # don't mess with this variable (counts number of rolls) countT = 0 # ...or this one either (counts rolls equal to target) while countR < numRolls: countR = countR + 1; if random.randint(1,numSides)==target: countT = countT + 1 print("You rolled a", target, "exactly", countT, "times in", countR, \ "rolls of a", numSides, "sided die.") if target == 0: print("Did you forget to change the target variable?") else: print("That's {:.2f}%. Well done!".format(countT*100/numRolls)) # end of Example 2
# Example 3: Rolling Until you reach a target number import random numSides = 6 # change this to roll dice with a different number of sides target = 1 # change this to change the target number count = 0 # don't mess with this variable (counts number of rolls) while random.randint(1,numSides) != target: count = count + 1 print("It took you {} roll(s) of a {}-sided die to roll a {}." \ .format(count+1, numSides, target)) # end of Example 3
# Example 4: Repeating example 3 a bunch of times to get an average import random numSides = 6 # change this to roll dice with a different number of sides numTrials = 20 # change this to change the number of attempts target = 1 # change this to change the target number count = 0 # don't mess with this variable (counts number of rolls) for x in range(numTrials): while random.randint(1,numSides) != target: count = count + 1 count = count + 1 print("In {} experiments, it took an average of {:.6f} rolls of a" \ .format(numTrials, count/numTrials), \ "{}-sided die to roll a {}.".format(numSides, target)) # end of Example 4
# Example 5: Hannah vs. Tyler import random numSides = 2 # a 2-sided die is usually called a 'coin' # Statistician's Riddle: What do you call a 1-sided die? Hmarks = 3 # number of markers Hannah owns Tmarks = 2 # number of markers Tyler owns count = 0 # don't mess with this variable (counts coin flips) print("Ready to start: the score is Hannah-{} Tyler-{}".format(Hmarks,Tmarks)) while Hmarks > 0 and Tmarks > 0: count = count + 1; if random.randint(1,numSides) == 1: Hmarks = Hmarks + 1; Tmarks = Tmarks - 1; print("Heads on flip {}: ".format(count),end=""); else: Hmarks = Hmarks - 1; Tmarks = Tmarks + 1; print("Tails on flip {}: ".format(count),end=""); print("the score is Hannah-{} Tyler-{}".format(Hmarks,Tmarks)) if (Hmarks > 0): print("\nHannah wins in {} flips.".format(count)); else: print("\nTyler wins in {} flips.".format(count)); # end of Example 5
# Example 6: Hannah vs. Tyler over and over again import random numSides = 2 # a 2-sided die is usually called a 'coin' # Answer to Riddle: A 1-sided die is called a marble. numTrials = 20 # the number of trials (you want this to be BIG!!) Hmarks = 3 # number of markers Hannah owns (change as needed) Tmarks = 2 # number of markers Tyler owns (change as needed) Hwins = 0 # don't mess with this variable (counts Hannah's wins) count = 0 # ..or this one (counts total coin flips) total = Hmarks + Tmarks # (this variable is just for convenience) for x in range(numTrials): H = Hmarks; while H > 0 and H < total: count = count + 1 if random.randint(1,numSides) == 1: H = H + 1 else: H = H - 1 if H > 0: Hwins = Hwins + 1; print("In", numTrials, "games, where Hannah starts with", Hmarks, \ "markers and Tyler starts with", Tmarks, "markers, Hannah won", \ Hwins, "times.") print("That's a winning percentage of {:.6f}.".format(Hwins/numTrials)) print("The average length of a game was {:.6f} flips.".format(count/numTrials)) # end of Example 6