Hint: Make sure your while loop conditions eventually evaluate to a false value, or they’ll never stop! Typing Ctrl-C will stop infinite loops in the interpreter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
>>> n = 3 >>> while n >= 0: ... n -= 1 ... print(n) ______ >>> positive = 28 >>> while positive: ... print("positive?") ... positive -= 3 ______ >>> positive = -9 >>> negative = -12 >>> while negative: ... if positive: ... print(negative) ... positive += 3 ... negative += 3 ______
Q3: WWPD: Veritasiness
Use Ok to test your knowledge with the following “What Would Python Display?” questions:
>>> True and 13 ______ >>> False or 0 ______ >>> not 10 ______ >>> not None ______ >>> True and 1 / 0 and False ______ >>> True or 1 / 0 or False ______ >>> True and 0 ______ >>> False or 1 ______ >>> 1 and 3 and 6 and 10 and 15 ______ >>> -1 and 1 > 0 ______ >>> 0 or False or 2 or 1 / 0 ______ >>> not 0 ______ >>> (1 + 1) and 1 ______ >>> 1/0 or True ______ >>> (True or False) and False ______
Q4: Debugging Quiz
The following is a quick quiz on different debugging techniques that will be helpful for you to use in this class. You can refer to the debugging article to answer the questions.
Code Writing Questions
Q5: Falling Factorial
Let’s write a function falling, which is a “falling” factorial that takes two arguments, n and k, and returns the product of k consecutive numbers, starting from n and working downwards. When k is 0, the function should return 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
def falling(n, k): """Compute the falling factorial of n to depth k.
>>> falling(6, 3) # 6 * 5 * 4 120 >>> falling(4, 3) # 4 * 3 * 2 24 >>> falling(4, 1) # 4 4 >>> falling(4, 0) 1 """ "*** YOUR CODE HERE ***" i = 0 res = 1 while i < k: res *= (n-i) i += 1 return res
Q6: Sum Digits
Write a function that takes in a nonnegative integer and sums its digits. (Using floor division and modulo might be helpful here!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
def sum_digits(y): """Sum all the digits of y.
>>> sum_digits(10) # 1 + 0 = 1 1 >>> sum_digits(4224) # 4 + 2 + 2 + 4 = 12 12 >>> sum_digits(1234567890) 45 >>> a = sum_digits(123) # make sure that you are using return rather than print >>> a 6 """ "*** YOUR CODE HERE ***" count = 0 while y > 0: count += y % 10 y = y//10 return count
Extra Practice
These questions are optional and will not affect your score on this assignment. However, they are great practice for future assignments, projects, and exams. Attempting these questions can be valuable in helping cement your knowledge of course concepts.
Q7: WWPD: What If?
Use Ok to test your knowledge with the following “What Would Python Display?” questions:
1
python3 ok -q if-statements -u✂️
Hint: print (unlike return) does not cause the function to exit.
def double_eights(n): """Return true if n has two eights in a row. >>> double_eights(8) False >>> double_eights(88) True >>> double_eights(2882) True >>> double_eights(880088) True >>> double_eights(12345) False >>> double_eights(80808080) False """ "*** YOUR CODE HERE ***" count = 0 while n > 0: if n % 10==8: count += 1 n = n//10 return count==2