These will not be collected. They are not guaranteed to be 100% of the test
material. You can do these and discuss them with your TA or Dr. Keen.
- Write Python code to compute xn by using a for loop.
Assume you have values in x and n. Do not use the exponentiation operator.
Hint: use an accumulator.
- Write a for loop that would generate the output below:
0.0 - 0.1
0.1 - 0.2
0.2 - 0.3
0.3 - 0.4
etc.
0.9 - 1.0
- Write some code where you use a for loop
to input 10 numbers and report the average of only the POSITIVE ones.
Include the initializations needed.
-
The graphics library uses objects and methods. Can you tell the difference between them? do you know the syntax for calling a method?
- If you had the statement a.b(c) which one is the object?
which one is the argument? which one is the method?
- Describe the coordinate system for the graphics library.
Where is the origin by default? Can you change where the origin is?
- Describe the two different uses of the getMouse method in the graphics
library.
- Sketch what this code will draw (you do NOT have to be
precise!) Assume it is part of a valid program.
wn = GraphWin()
tt = Point(50,50)
c2 = Circle (tt, 25)
c2.draw(wn)
- Know the difference between logical and relational operators.
- Put parentheses in the expression below so that they show
what order the operations will be done in:
a < 0 and b >= 5 + y or z > 3 and q == p
- Write an expression logically equivalent to
not (a > b and c > d) which does not use the not operator. "logically equivalent"
means that when one expression is true, the other is, and the same with false.
- What is the difference in operation between a "=" operator
and a "==" operator?
- Write a design for this problem: read in three numbers and
output the ones that are larger than their average. This should be
done with pseudocode, NOT code! no Python at all,
just English. Number the steps if you like.
- Write the code for the design in the previous problem.
-
All positive integers below 10 that are multiples of 3 or 5
are 3, 5, 6, 9. The sum of these numbers is 23.
Write some code to find the sum of all positive multiples of 3 or 5 below 1000.
- Fill in the blanks so that the code uses sentinel logic.
The sentinel value is 999.
num = int(______________________)
while ______________________:
print(num)
num = int(input())
- What's a flag? What values does a flag have?
- What causes an infinite loop?
- What's the difference between a control structure and a data structure?
- Name the basic control structures.
- What's dead code?
- What causes an infinite loop? Why is it difficult to write an infinite
for loop in Python?
- What are the guarantees true for EACH control structure? What is the guarantee that
is true for ALL control structures?
- Debugging what are the jobs of a debugger?
- What is a breakpoint?
- What is a call stack?
- What does a "step over" button do for you in debugging? "step into"? "step out of"?
- Trace this code: (fill in the table below)
n = 0 # line 1
while n < 10: # line 2
print(n, end=" ") # line 3
n = n + 2 # line 4
Line # | n | output |
| | |
| | |
| | |
| | |
| | |
| | |
and so on
-
Trace this code: Fill out a table that shows line numbers
executed, variables and output.
b = 7 # line 1
x = 5 # line 2
y = x * 15 # line 3
print(b, x, y) # line 4
a = 3 * b + 9 # line 5
x = y + 3 # line 6
y = x # line 7
print (b, x, a, y, end=" ") # line 8
a = 5 * b # line 9
print (a) # line 10
- Write some code that would input an integer number from the user which MUST be between 1 and 10,
exclusive (not 1 and not 10).
If the user does not input a number in that range, tell them so and ask
for it again. Repeat until the number is in the right range.
- Do the same problem as the previous one, but give the user no more
than 3 chances to enter the number, then tell them the value 5 will
be used and leave the loop.
- What library do you import for random numbers?
- What does the seed() function do?
- What does randrange() give you in its 3 versions?
- What does randint() do? what are its arguments?
- What does random() return?
- What does "deterministic" mean?
- What is the output of this code with speed = 55?:
if (speed < 40)
print("slow")
elif (speed < 55)
print("legal")
else
print("ticket")
Write a test plan for this code.
- How many times will each of these for loops repeat their body?
What values will the variable i go through? What _type_ will i be?
- for i in range(10):
- for i in range(3, 10, 2):
- for i in range(2, 5):
- for i in [2,3,5,1,0]:
- for i in "APPLE":
- for i in range(10, 4, 3):
- for i in range(5, 1, -1):
-
What are DeMorgan's Laws? what are they used for? Use them to simplify
not ( num > 50 or ct == 8)
"logically equivalent"
means that when one expression is true, the other is, and the same with false.
- What are the test cases you use for testing a loop?
what are the test cases you use for testing an if/else?
- Write some code that will use a flag to find out if ANY numbers input by the user are
larger than 500. There will be 10 numbers input from the keyboard.
The code should report afterward that "There were no numbers greater than 500" or
"there was at least one number greater than 500". Is this an ANY or an ALL pattern for flags?
- What are the "three P's"?
- Write a program which will play a guessing game with the user. The user inputs an integer from 1-6 inclusive. The program generates a random number in the same range, 1 through 6 inclusive. If the number the user entered is the same as the random number, they win 3 times the number in dollars. If they don't match, they don't win anything.
Write the program to run this guessing game 5 times. Keep a running total of the number of dollars they won and report it at the end.
- What is an alias? What is the output of the
code below?
p1 = [1, 3, 5]
p2 = p1
p2[2] = 7
print(p1)
- Describe a flag variable. What type should it be?
why? List the three steps you do with a flag.
What is the difference between the ANY pattern and the ALL pattern?
- Describe the "dot notation" and what it means. What kinds of
objects did you see this semester? What kinds of methods?
- Why are each of these an infinite loop?
a. Answer = 'y'
while Answer == 'y':
n +=1
ans = input("another loop? y/n")
b. i = n+1
while i != n:
i +=1
print(i)
- Correct the logic errors in the code below.
# Add numbers entered until sentinel -1 is encountered.
n = int(input("enter a number"))
sum = 0
while n == -1:
sum = sum + n
n = int(input("enter a number"))
- Which kind of loop do you want in these circumstances?
- You are writing a game where the user will guess numbers until
they guess the one the computer picked at random.
- You are testing a student to see if they know the capitals of
all 50 states. They only get one chance per state.
- You are drawing a standard-sized chessboard.
- What's the output?
count = 1
sum = 0
for number in range(2):
while count > number:
sum = sum + 4
count = count - 1
print(sum)
- What's the output?
list3 = [0,0,0]
for i in range(2):
for j in range(2):
if i == j:
list3[i] = i-j
else:
list3[j] = j-i
print(list3)