Cs133p chapter 3, 4, 5 and 6 book

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

In Python a module is:

(A) A file containing Python definitions and statements intended for use in other Python programs. ---> A module can be reused in different programs.

To find out information on the standard modules available with Python you should:

(A) Go to the Python Documentation site. ---> The site contains a listing of all the standard modules that are available with Python.

The loop body will execute (and print one line) for each of the 6 elements in the list [5, 4, 3, 2, 1, 0].

(A) They are indented to the same degree from the loop header.

turtle-2-7: True or False: A Turtle object can have any name that follows the naming rules from Chapter 2.

(A) True ---->! In this chapter you saw one named alex and one named tess, but any legal variable name is allowed.

Which statement allows you to use the math module in your program?

(A) import math --> The module must be imported before you can use anything declared inside the module.

Which of the following is the correct way to reference the value pi within the math module. Assume you have already imported the math module. (A) math.pi (B) math(pi) (C) pi.math (D) math->pi

(A) math.pi---->To invoke or reference something contained in a module you use the dot (.) notation.

The correct code to generate a random number between 1 and 100 (inclusive) is: (A) prob = random.randrange(1, 101) (B) prob = random.randrange(1, 100) (C) prob = random.randrange(0, 101) (D) prob = random.randrange(0, 100)

(A) prob = random.randrange(1, 101) --->This will generate a number between 1 and 101, but does not include 101.

turtle-6-5: In the following code, what is the value of number the second time Python executes the loop? for number in [5, 4, 3, 2, 1, 0]: print("I have", number, "cookies. I'm going to eat one.")

(B) 4-->Yes, Python will process the items from left to right so the first time the value of number is 5 and the second time it is 4.

One reason that lotteries don't use computers to generate random numbers is: (A) There is no computer on the stage for the drawing. (B) Because computers don't really generate random numbers, they generate pseudo-random numbers. (C) They would just generate the same numbers over and over again. (D) The computer can't tell what values were already selected, so it might generate all 5's instead of 5 unique numbers.

(B) Because computers don't really generate random numbers, they generate pseudo-random numbers. ---> Computers generate random numbers using a deterministic algorithm. This means that if anyone ever found out the algorithm they could accurately predict the next value to be generated and would always win the lottery.

True / False: All standard Python modules will work in activecode.

(B) False Only turtle, math, and random have been ported to work in activecode at this time.

turtle-2-1: True or False: You can only have one active turtle at a time. If you create a second one, you will no longer be able to access or use the first.

(B) False--You can create and use as many turtles as you like. As long as they have different names, you can operate them independently, and make them move in any order you like. If you are not totally convinced, try interleaving the instructions for alex and tess in ActiveCode box 3.

turtle-2-5: Consider the following code: What does the line "import turtle" do? import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) alex.left(90) alex.forward(75)

(B) It defines the module turtle which will allow you to create a Turtle object and draw with it. -->This line imports the module called turtle, which has all the built in functions for drawing on the screen with the Turtle object.

Which module would you most likely use if you were writing a function to simulate rolling (A) the math module (B) the random module (C) the turtle module (D) the game module

(B) the random module ---> You would likely call the function random.randrange.

turtle-6-3: In the following code, how many lines does this code print? for number in [5, 4, 3, 2, 1, 0]: print("I have", number, "cookies. I'm going to eat one.")

(C) 6-->The loop body will execute (and print one line) for each of the 6 elements in the list [5, 4, 3, 2, 1, 0].

Consider the following code: for aColor in ["yellow", "red", "green", "blue"]: alex.forward(50) alex.left(90)

(C) Draw one side of a square.--->The body of the loop only draws one side of the square. It will be repeated once for each item in the list. However, the color of the turtle never changes.

Which direction does the Turtle face when it is created?

(C) East

Which of the following best describes what is wrong with the previous example?

(C) Python is doing string concatenation, not integer addition.

turtle-2-6: Why do we type turtle.Turtle() to get a new Turtle object?

(C) The first "turtle" (before the period) tells Python that we are referring to the turtle module, which is where the object "Turtle" is found.

for name in ["Joe", "Amy", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]: print("Hi", name, "Please come to my party on Saturday!")

Line 2 is the loop body. The loop body is always indented. The indentation determines exactly what statements are "in the loop". The loop body is performed one time for each name in the list.

The list of names in the square brackets is called a

Python list.

As a program executes, the interpreter always keeps track of which statement is about to be executed.

We call this the control flow, or the flow of execution of the program

In Python, the for statement

allows us to write programs that implement iteration. As a simple example, let's say we have some friends, and we'd like to send them each an email inviting them to our party. We don't quite know how to send email yet, so for the moment we'll just print a message for each friend.

Name errors

almost always mean that you have used a variable before it has a value. Often NameErrors are simply caused by typos in your code. They can be hard to spot if you don't have a good eye for catching spelling mistakes. Other times you may simply mis-remember the name of a variable or even a function you want to call.

ParseError

happen when you make an error in the syntax of your program. Syntax errors are like making grammatical errors in writing. If you don't use periods and commas in your writing then you are making it hard for other readers to figure out what you are trying to say.

One of the most important skills you need to aquire to complete this book successfully

is the ability to debug your programs.

A basic building block of all programs is to be able to repeat some code over and over again. In computer science, we refer to this repetitive idea as

iteration.

name in this for statement is called the(p.4.4)

loop variable

ValueError

occur when you pass a parameter to a function and the function is expecting a certain limitations on the values, and the value passed is not compatible.

TypeErrors

occur when you you try to combine two objects that are not compatible. For example you try to add together an integer and a string.

Control flow until now has been strictly top to bottom, one statement at a time. We call this type of control

sequential.

what modules are

they are data objects, just like any other data in Python. Module objects simply contain other Python elements.


Kaugnay na mga set ng pag-aaral

Employee Dissatisfaction and Grievances Ch. 9

View Set

Biology : 9. ECOLOGY, POLLUTION, AND ENERGY

View Set

Exam 3 Spring 18 Mental Health Varcarolis 14-17, 28

View Set

Level D Unit 10 Choosing the Right Word

View Set

Chapter 11 : Liquids, Solids, and Intermolecular Forces

View Set