11/12/24, 8:55 PM assignment4_part2
Assignment4_part2 (Score: 6.0 / 6.0)
1. Test cell (Score: 1.0 / 1.0)
2. Test cell (Score: 1.0 / 1.0)
3. Test cell (Score: 1.0 / 1.0)
4. Test cell (Score: 1.0 / 1.0)
5. Test cell (Score: 1.0 / 1.0)
6. Test cell (Score: 1.0 / 1.0)
Assignment 4 (part two)¶
Advanced Functions
Create a function called mult that has two parameters, the first is required and should be an
integer, the second is an optional parameter that can either be a number or a string but whose
default is 6. The function should return the first parameter multiplied by the second.
In [1]:
Student's answer (Top)
def mult(req,op = 6):
return(req * op)
print(mult(5))
30
In [2]:
Grade cell: cell-3ceeb3dd921f3084 Score: 1.0 / 1.0 (Top)
Write a function greeting that has three parameters greeting , name , excl . name is required
while the optional parameter greeting has the default value of "Hello " and the optional parameter
excl has the default value of "!". The function returns the string greeting name excl .
greeting("Bob") should return Hello Bob!
https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=HlEpQ54SEe-CxAr_yRz-Qw&feedbackType=HTML 1/5
11/12/24, 8:55 PM assignment4_part2
In [3]:
Student's answer (Top)
# YOUR CODE HERE
def greeting(name, greeting="Hello ", excl="!"):
return greeting + name + excl
print(greeting("Bob"))
print(greeting(""))
print(greeting("Bob", excl='!!!'))
Hello Bob!
Hello !
Hello Bob!!!
In [4]:
Grade cell: cell-6518a18258ba7480 Score: 1.0 / 1.0 (Top)
Write a function sum that has two parameters intx , intz . intx is required while optional
parameter intz has the default value of 5. sum returns the sum of the two parameters.
In [5]:
Student's answer (Top)
# YOUR CODE HERE
def sum(intx, intz=5):
return intx + intz
# Call with both parameters
result1 = sum(3, 4)
print(result1) # Output: 7
# Call with only the required parameter
result2 = sum(3)
print(result2) # Output: 8 (since intz defaults to 5)
7
8
https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=HlEpQ54SEe-CxAr_yRz-Qw&feedbackType=HTML 2/5
11/12/24, 8:55 PM assignment4_part2
In [6]:
Grade cell: cell-80e59ab5e4311ddf Score: 1.0 / 1.0 (Top)
Write a function, test , that takes in three parameters: a required integer, an optional boolean
whose default value is True , and an optional dictionary, called dict1 , whose default value is
{2:3, 4:5, 6:8} . If the boolean parameter is True , the function should test to see if the integer is
a key in the dictionary. The value of that key should then be returned. If the boolean parameter is
False, return the boolean value False .
In [7]:
Student's answer (Top)
# YOUR CODE HERE
def test(integer, boolean=True, dict1={2: 3, 4: 5, 6: 8}):
if boolean:
return dict1.get(integer, None) # Returns the value if integer is a key, No
else:
return False
print(test(2)) # Output: 3 (because 2 is a key in the dictionary, and its value is
print(test(4)) # Output: 5 (because 4 is a key in the dictionary, and its value is
print(test(1)) # Output: None (because 1 is not a key in the dictionary)
print(test(1, False)) # Output: False (because the boolean is False)
3
5
None
False
In [8]:
Grade cell: cell-bc67a36e09a80492 Score: 1.0 / 1.0 (Top)
https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=HlEpQ54SEe-CxAr_yRz-Qw&feedbackType=HTML 3/5
11/12/24, 8:55 PM assignment4_part2
Write a function called checkingIfIn that takes three parameters. The first is a required parameter,
which should be a string. The second is an optional parameter called direction with a default
value of True . The third is an optional parameter called d that has a default value of {'apple':
2, 'pear': 1, 'fruit': 19, 'orange': 5, 'banana': 3, 'grapes': 2, 'watermelon': 7} .
When the second parameter is True , it should check to see if the first parameter is a key in the
third parameter; if it is, return True , otherwise return False .
But if the second paramter is False , then the function should check to see if the first parameter is
not a key of the third. If it’s not, the function should return True in this case, and if it is, it should
return False .
In [9]:
Student's answer (Top)
def checkingIfIn(string, direction=True, d={'apple': 2, 'pear': 1, 'fruit': 19, 'or
if direction:
# Check if string is a key in dictionary d
return string in d
else:
# Check if string is NOT a key in dictionary d
return string not in d
print(checkingIfIn("apple")) # Output: True (because "apple" is a key in the dictio
print(checkingIfIn("cherry")) # Output: False (because "cherry" is not a key in th
print(checkingIfIn("apple", False)) # Output: False (because "apple" is a key, and
print(checkingIfIn("cherry", False)) # Output: True (because "cherry" is NOT a key,
True
False
False
True
In [10]:
Grade cell: cell-6b88ea6b5c471994 Score: 1.0 / 1.0 (Top)
We have provided an alternative function checkingIfIn_2 . If the first input parameter is in the third
input parameter (a dictionary), then the function returns the associated value, and otherwise, it
returns False . Follow the instructions in the active code window for specific variable assignmemts.
https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=HlEpQ54SEe-CxAr_yRz-Qw&feedbackType=HTML 4/5
11/12/24, 8:55 PM assignment4_part2
In [11]:
Student's answer (Top)
def checkingIfIn(a, direction = True, d = {'apple': 2, 'pear': 1, 'fruit': 19, 'ora
if direction == True:
if a in d:
return d[a]
else:
return False
else:
if a not in d:
return True
else:
return d[a]
# Call the function so that it returns False and assign that function call to the va
c_false = checkingIfIn("")
# Call the fucntion so that it returns True and assign it to the variable c_true
c_true = checkingIfIn("",False)
# Call the function so that the value of fruit is assigned to the variable fruit_ans
fruit_ans = checkingIfIn("fruit")
# Call the function using the first and third parameter so that the value 8 is assig
param_check = checkingIfIn("litchi",d = {"litchi":8})
print(c_false)
print(c_true)
print(fruit_ans)
print(param_check)
False
True
19
8
In [12]:
Grade cell: cell-353668d69efbb7f8 Score: 1.0 / 1.0 (Top)
In [ ]:
This assignment was graded by mooc_python3:193e72412036, v1.15.070824
https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=HlEpQ54SEe-CxAr_yRz-Qw&feedbackType=HTML 5/5