---
tags: Python
---
# Python Self-Check 8
CS 1358 Introduction to Programming in Python
Fall Semester 2022
Prof. Pai H. Chou
Self-Check 1
Due Date: --
Answer the following questions to check your understanding of your material. Expect the
same kind of questions to show up on your tests. For this course, we use vi and vim
interchangeably.
## 1. Definitions and Short Answers
1. Given a function definition
```
def Double(n):
return n + n
and given a call to the function
x = Double(20)
```
What is a formal parameter? What is the actual parameter?
```
formal parameter:n
actual parameter:20
```
2. If you call the above function as
y = Double( Double(40 + Double(3)) - Double(7))
what is the value of y?
```
156
```
3. What is the difference between
d = Double
and
z = Double(20)
?
```
d is a reference of function object
z is a return value of function
```
4. If you do
print, input = input, print
z = input('enter a name: ')
What happens, and what is the value of z?
```
print out:enter a name:
```
5. Given the function
def DivMod(a, b):
return a // b, a % b
What does this function return?
```
a tuple
```
6. Are the following pairs equivalent (in terms of parameters received by the open() function for opening files)?
```
a.
fh = open('myfile.txt', 'r') and
fh = open(file='myfile.txt', mode='r')
//Yes
b.
fh = open('myfile.txt', 'r') and
fh = open(mode = 'r', name = 'myfile.txt')
//Yes
c.
fh = open('myfile.txt', 'r') and
fh = open('r', 'myfile.txt')
//No
d.
fh = open(file='myfile.txt', mode='r') and
fh = open(mode='r', file='myfile.txt')
//Yes
```
7. Consider the function declaration
```
def withTax(price, rate=0.05):
return price * (1 + rate)
```
Can you call this function in the following ways? If so, what is the result? If not, why not?
```
a. withTax(20)
//Yes, 21.0
b. withTax(rate=0.08, price=30)
//Yes, 32.4
c. withTax(price=20, 0.08)
//Yes, 21.6
d. withTax(price=20)
//Yes, 21.0
e. withTax(rate=0.08)
//No, without parameter price
f. withTax()
//No, without parameter price
g. withTax(20, 0.08)
//Yes, 21.6
```
8. Suppose you are given a function
```
def withMoreTax(price, rate = 0.05):
ans = price * (1 + rate)
rate += 0.01
return ans
```
What does the following code sequence print?
print(withMoreTax(20))
print(withMoreTax(20))
In other words, does the parameter rate's default value get changed on line 3 such that the next call gets the modified default value?
```
No, the default value is evaluated once for all
```
9. Are you allowed to define default values with non-constant expressions such as the following? If so, what does it do?
```
a.
import sys
def withNewTax(price, rate=input('enter a rate: ')):
return price * (1 + float(rate))
//Yes, input as a parameter rate
b
def withNewerTax(price, rates=[0.01, 0.02, 0.03]):
ans = price * (1 + rates[-1])
rates.pop()
return ans
//Yes, return price * (1 + rates[-1]) and if pop() the last element of rate will pop
c
def withNewestTax(price, rate=price):
return price * (1 + rate)
//No
```
10. uppose you have a function declared as
def totalTax(rate, *priceTags):
...
What is the value of rate and priceTags inside the function totalTax when you call it as
```
a. totalTax(0.05, 10, 20, 23, 18)
//rate = 0.05
priceTags = (10, 20, 23, 18)
b. totalTax(0.05, (10, 20, 23, 18))
//rate = 0.05
priceTags = ((10, 20, 23, 18),)
c. totalTax(0.05, *(10, 20, 23, 18))
//rate = 0.05
priceTags = (10, 20, 23, 18)
d. totalTax(*(0.05, 10, 20, 23, 18))
//rate = 0.05
priceTags = (10, 20, 23, 18)
e. totalTax(0.05)
//rate = 0.05
priceTags = ()
f. totalTax(0.05, 10)
//rate = 0.05
priceTags = (10)
```
11. Given the following function definition:
```
def totalTax(rate, **priceDict):
return sum(priceDict.values())* (1 + rate)
```
Are the following valid ways of calling the function? If so, what are the values of parameters rate and priceDict while inside the totalTax function, and what is the corresponding return value? If not valid, why not?
```
a. totalTax(0.05, apple=12, orange=8)
//Yes, rate = 0.05 priceDict = {'apple':12, 'orange':8} 21.0
b. totalTax(0.05, 12, 8)
//No, need have key
c. totalTax(0.05, 'apple'=12, 'orange'=8)
//No, string can't assign value
d. totalTax(0.05, 'apple':12, 'orange':8)
//No, Syntax error
e. totalTax(rate=0.05, priceDict={'apple':12, 'orange':8})
//No, Syntax error
f. totalTax(rate=0.05, **{'apple':12, 'orange':8})
//Yes, rate = 0.05 priceDict = {'apple':12, 'orange':8} 21.0
g. totalTax(rate=0.05, **priceDict={'apple':12, 'orange':8})
//No, Syntax error
h. totalTax(rate=0.05, priceDict=(('apple', 12),('orange', 8))
//No, Syntax error
i. totalTax(rate=0.05, **priceDict=(('apple', 12),('orange', 8))
//No, Syntax error
j. totalTax(apples=12, oranges=8, rates=0.05)
//No, without argment rate
```
12. Given the following function definition
```
def totalWithTax(rate, *items, **priceDict):
d={'apple':12,'orange':8, 'mango':3}
d.update(priceDict)
total = 0
for i in items:
total += d[i]
return total * (1 + rate)
```
Are the following valid ways of calling the function? If so, what are the values of parameters rate, items, and priceDict while inside the totalWithTax function, and what is the corresponding return value? If not valid, why not?
```
a. totalWithTax(0.05, 'apple', 'orange', 'mango')
//rate = 0.05, items = ('apple', 'orange', 'mango'), priceDict = {}
24.15
b. totalWithTax(0.05, 'apple', 'orange', 'mango', 'apple', 'apple')
//rate = 0.05, items = ('apple', 'orange', 'mango', 'apple', 'apple'), priceDict = {}
49.35
c. totalWithTax(0.05, 'apple', 'orange', 'mango', apple=3, orange=4, mango=5)
//rate = 0.05, items = ('apple', 'orange', 'mango'), priceDict = {'apple':3, 'orange':4, mango=5}
12.6
d. totalWithTax(0.05, 'apple', 'orange', 'mango', **{'apple':3, 'orange':4, 'mango':5})
//rate = 0.05, items = ('apple', 'orange', 'mango'), priceDict = {'apple':3, 'orange':4, mango=5}
12.6
e. totalWithTax(0.05, *('apple', 'orange', 'mango'))
//rate = 0.05, items = ('apple', 'orange', 'mango'), priceDict = {}
24.15
f. totalWithTax(0.05)
//rate = 0.05, items = (), priceDict = {}
0.0
```
13. Suppose you have two tuples
A = (1, 3, 7)
B = (2, 4, 8)
and you want to use the built-in function max() to find the largest element in the two tuples. Which of the following will correctly find the answer (which is 8)? Choose all that apply and explain why or why not
```
a. max(A, B)
//(2, 4, 8)
b. max(*A, *B)
//8
c. max(A + B)
//8
d. max(*(A+B))
//8
e. max(*A + *B)
//Syntaxerror
f. max((A, B))
//(2, 4, 8)
g. max(*(A, B))
//(2, 4, 8)
```
14. Given the following code
```
a = 3
def F():
print(a)
F()
```
What is printed when you run this code?
```
3
```
15. Given the following code
```
a = 3
def F():
a = 5
print(a)
F()
print(a)
```
What is printed when you run this code?
```
5
3
```
16. Given the following code
```
a = 3
def F():
print(a)
a = 5
F()
print(a)
```
But it gives an UnboundLocalError when you try to run it.
```
a. Why do you get this error?
violation of consistent binding rule!(lookup binding and definition binding must be consistent(in the same table), or else it is an error.)
b. How do you fix it if you want the function F to use the identifier a as defined on line 1?
global a before print(a)
```
17. Given the source code
```
D = { 'rate': 0.0 }
def totalWithTax(*names, **kv):
global D
total = 0.0
for name in names:
total += D[name]
for kw, val in kv.items():
D[kw] = val # overwrite dict entry
if kw != 'rate':
total += val
return total * (1 + D['rate'])
```
Are the following valid ways of calling the function totalWithTax? If so, what are the values of parameters names, and kv while inside the totalWithTax function, the values of the global variable D, and what is the corresponding return value? If not valid, why not? Assume you reload the code each time before executing the code below.
```
a. totalWithTax()
//D = {'rate': 0.0 }, names = (), kv = {}, 0.0
b. totalWithTax('apple')
//D = {'rate': 0.0 }, names = ('apple'), kv = {}, Keyerror(D don't have key apple)
c. totalWithTax(apple=20)+totalWithTax('apple')
//D = {'rate': 0.0, 'apple': 20}, names = ('apple'), kv = {'apple': 20}, 40
d. totalWithTax('orange', orange=15)
//D = {'rate': 0.0, 'orange': 15}, names = ('orange'), kv = {'orange': 15}, 15
e. totalWithTax(guava)
//D = {'rate': 0.0 }, names = ('guava'), kv = {}, Keyerror(D don't have key guava)
f. totalWithTax(rate=0.05, apple=10) + totalWithTax('apple', 'apple', 'apple')
//D = {'rate': 0.5, 'apple':10}, names = (apple', 'apple', 'apple'), kv = {'apple':10}, 42
g. totalWithTax(apple=10, orange=15)+totalWithTax('apple',guava=12)
//D = {'rate': 0.5, 'apple':10, 'orange':15, guava=12}, names = (apple'), kv = {'apple':10, 'orange':15} => {guava=12}, 47.0
h. totalWithTax(apple=10, orange=15) + totalWithTax(rate=0.05, guava=12) + totalWithTax(rate=0.02, 'apple', 'orange', 'guava')
//syntaxerror positional argument follows keyword argument
(rate is dict)
```
18. How do you write test code at the end of a module to test functions defined in the module when it is run as a top-level module (hint: '__main__'), but don't run the test case when it is imported by another module? Also, what construct should you use to check if a tested function returns the results as the correct answer?
```
if __name__ == '__main__':
assert ##function call you want to check and the value you want to check
```