Compound Statements and Random Numbers
Download
Report
Transcript Compound Statements and Random Numbers
Recap: If, elif, else
If <True condition>:
Do this
Elif <True condition>:
Otherwise, do this
Else:
Do this
Compound Statements and
Random Numbers
Compound Statements
You can use compound statements to make sure a block will only run if
there is more than one condition to be satisfied
if <condition 1> and <condition 2>
Then do this
Will only run if
both conditions
are true
if <condition 1> or <condition 2>
Then do this
Will only run if at
least one
condition is true
When you’re finished…
How could you make your program start over again and
re-prompt the user if the user put in a negative number
Add this to your existing code and email me .py file at
[email protected] and turn in your grade sheet
Read about random numbers and the random module:
pages 50-53
Creating while loops
While some condition is true, repeat something
In coding: you might want to create a quiz show game, while there
are questions left, keep playing the game.
The while loops lets you do this
What’s a while loop
Runs while a certain condition is true
Examples:
number = int(input("enter a number"))
while number !=5
number = int(input(“Guess another number”))
Generating Random Numbers
As much as users want consistent, predictable results from
programs, sometimes what make the programs exciting is
their unpredictability
Random numbers can supply an element of surprise
Python makes it easy to generate those random numbers
Python generates random numbers based on a formula, so
they are not truly random.
This kind of random generation is called pseudorandom
Introducing the Craps Roller Program
Open the Craps Roller Program from the Chapter 3 Folder
Craps Roller replicated the dice roll of the fast-paced, casino game
of craps. You don’t have to know anything about craps to
appreciate the program.
Craps Roller just simulates the roll of two, six-sided dice. It displayed
the value of each and their total.
To determine the dice values, the program uses functions that
generates random number. Figure 3.2 shows the program
Importing the random Module
The first line of code in the program introduces the
import statement
The statement allows you to import, or load,
modules, in this case, the random module:
import random
Modules
Modules are files that contain code meant to be used in other
programs. These modules usually group together a collection of
programming related to one area. The random module contains
functions related to generating random numbers and producing
random results.
When you import a module, it was either installed as part of your
Python installation, or was downloaded and installed later on, prior
to your running the program that imports it.
Generating Random Numbers
You can generate random numbers using:
randint()
randrange()
randint() Function
Produces a random integer
Notice the program doesn’t directly call randint()
It calls the function:
random.randint(1,6)
This accesses the function randint() through its module,
random
Dot notation
You can call a function from an imported module by giving the
module name, followed by a period, followed by the function call
itself (similar to string methods).
This method of action is called dot notation.
Using dot notation random.randint() means the function
randint() that belongs to the module random.
Dot notation can be used to access different elements of imported
modules
randint() function
Requires two integer argument values and returns a
random integer between those two values, which may
include either of the argument values.
By passing the values 1 and 6 to the function, you are
guaranteed to get back either 1,2,3,4,5 or 6
randrange() function
produces a random integer
Use a single, positive integer argument
The function returns a random integer from, and
including, 0, up to, but not including, that number
Example: random.randrange(6) produces either a
0,1,2,3,4, or 5
Where’s the 6 in the Craps Rollar?
randrange(6) is picking a random number from a group of 6
numbers – and the list starts with 0
If you add 1 to the result to get the right value for a die2:
die2=random.randrange(6)+1
As a result, die 2 gets either a 1,2,3,4,5, or 6.
Research:
What if you want to pick a random string out of a
list? What function would you use in python? Do
some research and turn in your answer.