Lecture 9 - Exercises

9A - import statements

  1. Which of the following programs will not cause an error when run?

    1.    import random
         random.randint(6, 10)
    2.    import random
         random.randint(10)
    3.    from math import sin
         sin(math.pi)
    4.    import math
         from math import cos
         cos(math.pi)
    5.    import math
         from math import cos
         math.cos(math.pi)
  2. What is the largest number that could be printed by the following program?

    import math
    import random
    
    print(math.ceil(random.uniform(random.random()*10, 30)))
  3. What is the difference between the random module’s randint and randrange functions?

9B - Turtles

As a reminder, for both of the following problems, I highly recommend trying to answer them before running the code to verify your answer.

  1. Describe in words what the following code draws on the Turtle’s canvas:

    import turtle
    t = turtle.Turtle()
    for i in range(30):
        t.forward(5)
        t.penup()
        t.forward(5)
        t.pendown()
  2. After the above code is run, what is its position (in x, y coordinates) on the canvas? You should be able to find the information needed to answer this question in the turtle module’s documentation.