2nd Semester Intro to Computer Science

What you will learn

Reading

Do Now

In your Console

Type the following code

import random
random.randint(0, 3)
random.randint(0, 3)
print(random.randint(0, 3))
print(random.randint(0, 3))
print(random.randint(0, 3))

In your Notebook

Respond to the following

  1. What does randint do?
  2. What do the values 0 and 3 do? Try changing those numbers, rerun the program, and write down what changed.
  3. What is the difference between random.randint(0,3) and print(random.randint(0,3))?

Lab

Practice importing random** — Use randint with different arguments. Simulate a dice roll, printing out to the user what number they rolled.

Look at the documentation of the random library — Experiment with another function (not randint) that returns a value.

Create a program that simulates a magic 8-ball

  1. Store all of the 8-ball’s possible responses (shown below) in a list
  2. Have the program prompt the user to ask the magic 8-ball a question
  3. then return and print a random response.

Magic 8-Ball Response Examples

Video Explanation

Magic 8 Ball

Bonus

Research the math library and create a program that finds the length of the hypotenuse of a right triangle given two sides.

Check your understanding

Pick an answer for each question. You can try again until you get it right.

1. What does random.randint(0, 3) do?

  • Always returns 3
  • Prints the numbers 0 through 3
  • Returns a random whole number from 0 to 3, including both ends
  • Returns a random decimal between 0 and 3

It picks a random integer between the two arguments, inclusive, and gives it back to the caller.

2. What must you do before you can call random.randint?

  • Write import random first
  • Define randint yourself with def
  • Nothing, every function is always available
  • Print the arguments

Code from the random library has to be imported before you can use it.

3. If a program contains only the line random.randint(1, 6) (no print), what do you see when it runs?

  • A random number from 1 to 6
  • Nothing, the value is returned but never displayed
  • An error
  • The text randint

A returned value is not shown unless you print it or store it.

Downloads