Computer Science Unit 6
What are the arguments to the user-defined function? (List arguments in the order they appear) def print_design(symbol, num): print((symbol + "-*-") * num) print(("-!-" + symbol) * num) print_design("#", 5) print_design("&", 3)
"#", 5, "&", 3
In the following code signature, highlight the entire names of any parameters that can accept a variable number of arguments (do not highlight the spaces or commas between parameters)
*ingredients, **substitutions
What does score equal on line 6? score = 0 def win(): score = 100 win() print(score)
0
What is wrong with this code? def can_enter(passphrase): if passphrase == "open sesame": return True print("That is correct!") else: return False result = can_enter("open sesame")
A. "That is correct!" will never print
Where in your program can you access global variables?
A. Anywhere in the program
What will print to the console when this program is run? def add_recipe_groceries(grocery_list): ingredients = ["eggs", "milk", "sugar"] grocery_list = ingredients my_list = ["steak", "carrots", "apple juice"] add_recipe_groceries(my_list) print(my_list)
A. ["steak", "carrots", "apple juice"]
What order will these print statements run?
A. blue, red, green
What is a namespace?
An internal dictionary that tracks names along with their values
How can you return multiple values from a function?
B. Put the values you want to return in a collection, then return the collection
What will the value of the parameter scores be when running the following code? def calculate_grade(name, period, *scores): total = 0 for score in scores: total += score average = total/len(scores) print(name + " in period " + str(period) + " scored " + str(average)) calculate_grade("Caolan", 2, 78, 93, 82, 100, 87)
C. (78, 93, 82, 100, 87)
What will happen if we run the following program? (You can assume that there is no other code in the file.) def first_function(): print("Hello World!")
C. Nothing will print to the console
Which of the following would be the correct function signature for a function that took in any number of students, a teacher name, and a class subject?
C. def class_creation(teacher, subject, *students):
Which of the following is a benefit of organizing code into functions?
D. All of the above
What will happen when we run the following code? def introduction(name, age): print("Hello, my name is " + name) print("I am " + str(age) + " years old") introduction(age=14, name="Jillian")
D. The code will run, and the name and age will be passed into the appropriate parameters
What is being returned from the print_design function? def print_design(symbol, num): print((symbol + "-*-") * num) print(("-!-" + symbol) * num) print_design("+", 1)
None
Which of the following is not an example of a side effect?
Returning a value
What will print to the console when this program is run? def add_student(students, new_student_id): if new_student_id not in students: students.append(new_student_id) else: print("That student has already been added") my_students = [124, 341, 762] add_student(my_students, 147) print(my_students)
[124, 341, 762, 147]
Add a line after the function declaration so that the function updates the global count variable
count = 0 def guess_my_name(name): global count if name == "Rumpelstilskin": print("You got it!") else: count += 1
Generally people don't have food allergies. Update this function so that nut and wheat allergies default to False when the function is called
def allergy_information(name, nuts=False, wheat=False): if not nuts and not wheat: print(name + " can eat foods with nuts and/or wheat") else: print(name + " has a food allergy")
Rewrite this code to get rid of the vote variable by calling the function within the condition.
def can_vote(age, registered): if age < 18: return False if not registered: return False return True if can_vote(18, True): print("You are eligible to vote") else: print("Sorry, you cannot vote at this time")
Highlight the entire function signature
def greeting(name):
Rewrite the 2 function calls to remove any arguments that can be correctly filled by the function's default parameters
def pet_info(name, cats=0, dogs=0, fish=0): print(name + " has the following pets:") print(" * " + str(cats) + " cats") print(" * " + str(dogs) + " dogs") print(" * " + str(fish) + " fish") pet_info("Andrea", 1) pet_info("Toby")
Add a line to this function definition so that it returns the total summed value.
def sigma(num): total = 0 while num >= 1: total += num num -= 1 return total calc = sigma(5) print(calc)
Write a function call for someone named "Wanda" who has 0 cats, 0 dogs, and 10 fish. Do not pass in values for default parameters that match the argument. def pet_info(name, cats=0, dogs=0, fish=0): print(name + " has the following pets:") print(" * " + str(cats) + " cats") print(" * " + str(dogs) + " dogs") print(" * " + str(fish) + " fish")
pet_info("Wanda", fish=10)
What are the parameters in the following code snippet? (List parameters in the order they appear) def print_design(symbol, num): print((symbol + "-*-") * num) print(("-!-" + symbol) * num) print_design("#", 5) print_design("&", 3)
symbol, num