0% found this document useful (0 votes)
3 views

Copy of Check-in Week 2

The document presents exercises related to Python programming, focusing on loops, Fibonacci numbers, and function naming. It includes tasks for drawing shapes using the turtle module, completing incomplete code for generating Fibonacci sequences, and improving function names in existing programs. Additionally, it encourages understanding of how loops and variables interact within Python code.

Uploaded by

assismyfavfood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Copy of Check-in Week 2

The document presents exercises related to Python programming, focusing on loops, Fibonacci numbers, and function naming. It includes tasks for drawing shapes using the turtle module, completing incomplete code for generating Fibonacci sequences, and improving function names in existing programs. Additionally, it encourages understanding of how loops and variables interact within Python code.

Uploaded by

assismyfavfood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Can Computers Think?

(CSC 106)​ ​ ​ ​ ​ Checking your understanding

Consider the following Python program:

import turtle

for size in range(10, 60):


for side_count in range(0, 4):
turtle.forward(size)
turtle.left(90)

1.​ Draw what shape the turtle draws when the inner loop is run once. Your sketch does not have to
be to scale (any size is ok).​




2.​ Sketch what shape the turtle draws when the whole program is executed.​





3.​ Explain your answer to Question 2. How do the outer loop and the inner loop work together to
produce the picture you sketched?

1
Consider the following (incomplete) Python program:

x = int(input("Please input a positive integer: "))


counter = x
for step in range(0, 10):

1.​ Complete the missing lines of code above so that the program will produce the following output if
the user inputs 3.

3
6
9
12
15
18
21
24
27
30

Notes: You do not need to introduce any additional variables.​


There is more than one way to achieve this result.

2
The Fibonacci numbers are the following sequence of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
The first two numbers are 0 and 1. After that each number can be calculated by adding the previous two
together. For example, the third number is 1 (0+1), the fourth number is 2 (1+1), the fifth number is 3
(2+1), etc.

They are named Fibonacci numbers after the Italian mathematician who first described them in Europe.
Patterns based on the Fibonacci sequence show up in surprisingly many contexts in math, biology, and art.
(See the Wikipedia entry if you want to know more.)

Consider the following (incomplete) Python program to list the first few Fibonacci numbers. The
user gets to determine how many Fibonacci numbers to list.

1.​print (“How many Fibonacci numbers would you like to see?”)


2.​limit = int (input(“Please input a number greater than 2: ”))
3.​
4.​fib_0 = 0
5.​fib_1 = 1
6.​
7.​print(fib_0)
8.​print(fib_1)
9.​for count in range( ):
10.​ next_fib =
11.​ print(next_fib)

Questions:
For questions 1-4, assume that the program has been completed and functions correctly.
1.​ Draw a memory diagram that shows the<variable name>-<value/object>
associations after line 5 has been executed. Assume that the user inputted 10. Also show what will
have been printed to the screen at that point.​



3
2.​ Draw a memory diagram that shows the <variable name>-<value/object>
associations that should exist after line 11 has been executed for the first time. Also show what
line 11 will print to the screen when it is executed for the first time.​






3.​ What should the <variable name>-<value/object> associations be when line 11 has
been executed for the second time? Draw a memory diagram.​





4.​ Draw a memory diagram that shows the <variable name>-<value/object>


associations after the last number has been printed (i.e. at the end of the program).​





5.​ Complete the code above so that the program prints the first n Fibonacci numbers, where n is
determined by the user. You may need to add new lines of code.

4
Consider two Python programs shown on page 6.
Both programs will cause Reeborg to harvest all
of the carrots in the Harvest 1 world:

Questions:

1.​ Both programs define a number of functions.


Unfortunately, the program designers were
transported from the bad old days when names
needed to be as short as possible, so the names
they chose for their functions are essentially
meaningless. Please try to come up with better
names for each of the functions that they
defined.

current name​ ​ better name​ ​ ​ ​ ​ ​ ​ ​ ​

a.​ ………………………………………………………………………………………………

b.​ ………………………………………………………………………………………………

c.​ ………………………………………………………………………………………………

d.​ ………………………………………………………………………………………………

e.​ ………………………………………………………………………………………………

f.​ ………………………………………………………………………………………………

g.​ ………………………………………………………………………………………………

h.​ ………………………………………………………………………………………………

5
2.​ Now that you’ve come up with better names for the functions, which of the solutions do you think

is better and why?​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

……………………………………………………………………………………………………….​

6
1 def turn_right(): 1 def turn_right():
2 ​ turn_left() 2​ turn_left()
3 ​ turn_left() 3​ turn_left()
4 ​ turn_left() 4​ turn_left()
5 5
6 def a(): 6 def e():
7 ​ move() 7​ move()
8​ turn_left() 8​ turn_left()
9​ move() 9​ move()
10​ move() 10​ move()
11​ turn_right() 11​ turn_right()
12​ move() 12​ move()
13 13​ take()
14 def b(): 14
15​ for carrot in range(0, 6): 15 def f():
16​ ​ take() 16​ for carrot in range(0, 5):
17​ ​ move() 17​ ​ move()
18 18​ ​ take()
19 def c(): 19
20​ turn_left() 20 def g():
21​ move() 21​ f()
22​ turn_left() 22​ turn_left()
23​ move() 23​ move()
24 24​ turn_left()
25 def d(): 25​ take()
26​ turn_right() 26
27​ move() 27 def h():
28​ turn_right() 28​ f()
29​ move() 29​ turn_right()
30 30​ move()
31 a() 31​ turn_right()
32 for double_row in range(0, 32​ take()
3): 33
33​ b() 34 e()
34​ c() 35 for double_row in range(0,
35​ b() 2):
36​ d() 36​ g()
37​ h()
38 g()
39 f()

You might also like