Unit 7: Functions
What is output when the user enters -2?
1
What is output by the following program? def sample(val): val = val * 10 #MAIN n = 6 sample(n) print(n)
6
We call the central part of the program ____________.
Main
The ___________ keyword is used to return a value from a function.
return
The _____________ keyword tells the subprogram what value to return.
return
Suppose we add the following line of code to our program: print(mystery(x)) What is output when the user enters 1, 1, and 1?
-1
For Questions 1-4, consider the following code: def mystery1(x): return x + 2def mystery2(a, b = 7): return a + b#MAINn = int(input("Enter a number:"))ans = mystery1(n) * 2 + mystery2 (n * 3)print(ans) What is output when the user enters -4?
-9
What is output by the following program? def mult(a, b = 1, c = 1): print(a * b * c) mult(2, 5)
10
What is output by the following program? def sample(val): val = val - 8 #MAIN n = 16 sample(n) print(n)
16
For Questions 2-4, consider the following program: def tryIt(a, b = 7):return a + b#MAINn = int(input('Enter a number: '))ans = tryIt(n) * 2print (ans) What is output if the user enters 2?
18
Consider the following code: def calc(num1, num2 = 0): return(num1 + num2) #MAIN val1 = int(input(" ")) val2 = int(input(" ")) print(calc(val1)) What is output when the user enters 19 and 18?
19
Consider the following code: def sample(val): val = val + 5# MAINn = 19sample(n)print(n) What is output?
19
What is output when the user enters 3?
26
What is output if the user enters 7?
28
Suppose we add the following line of code to our program: print(mystery(x, y)) What is output when the user enters 5, 8, and 2?
3
For questions 5-8, consider the following code: def mystery(a, b = 8, c = -6): return 2 * b + a + 3 * c#MAINx = int(input("First value: "))y = int(input("Second value: "))z = int(input("Third value: ")) Suppose we add the following line of code to our program: print(mystery(x, y, z)) What is output when the user enters 4, 3, and 7?
31
Suppose we add the following line of code to our program: print(mystery(x, y, z)) What is output when the user enters 8, 6, and 4?
32
Consider the following code: def mystery (a, b = 8, c = -6): return a + b + c #MAIN x = int(input("First value: ")) y = int(input("Second value: ")) z = int(input("Third value: ")) print(mystery (x, y, z)) What is output when the user enters 30, 2 and 2?
34
Consider the following code: def calc(num1, num2 = 0): print(num1 + num2) #MAIN val1 = int(input(" ")) val2 = int(input(" ")) calc(val1, val2) What is output when the user enters 19 and 18?
37
Consider the following program: def sample(val): val = val - 8 #MAIN n = 38 sample(n) print(n) What is output?
38
What is output if the user enters -5?
4
What is output when the user enters 9?
56
What is output by the following code? def mult(a, b = 2, c = 1): return (a * b * c) print(mult(3))
6
What is output by the following program? def mult(a, b = 1, c = 1): return(a * b * c) print(mult(2, 5, 6))
60
Consider the following program: def test_1(): test_3() print("A") def test_2(): print("B") test_1() def test_3(): print("C") What is output by the following line of code? test_1()
CA
The .upper and .lower functions can be used to change how a string is formatted. upper will print the letters as uppercase, and lower will print the letters as lowercase. Consider the following code: def mystery(v, w): print(w.upper() + " - " + v.lower()) #MAIN val1 = input("Enter a word: ") val2 = input("Enter a word: ") mystery(val1, val2) What is output if the user enters cat then dog?
DOG - cat
What is output by the following code? def mystery(w): if (w.upper() == w): return "TRUE" else: return "FALSE" print(mystery("Hello there!"))
FALSE
What is output by the following line of code? print(mystery("hello"))
False
What is output by the following line of code? print(mystery("zip code: 12345"))
False
What is output by the following code? def test(): print("Inside the sub")#MAINprint("In main")test()print("done.")
In main Inside the sub done.
What is output by the following program? def subtractOne(n): n = n - 1 print ("In the function: " + str(n)) #Main value = 5 subtractOne(value) print("In main: " + str(value))
In the function: 4 In main: 5
What is output by the following program? def test():print("Inside the function")#MAINtest()print("In main")print("done.")
Inside the function In main done.
What is output by the following program? def greeting(name = "?")return "Hello " + name + "!"#MAINprint(greeting("Joe"))
None of the above. The code has an error.
What is output by the following program? def greeting(name):print("Hello ", name.upper())greeting("Sara")
None of the above. The code has an error.
Which of the following parameters is optional? sample(a, b)
None of the parameters are optional
What does the "mystery" function do?
Returns TRUE if there are no letters in the string.
What is the primary use of functions?
To organize longer programs.
Which of the following is NOT a reason we use subprograms?
To simplify code
Why do we use functions?
To simplify code.
Which of the following applies to functions and describes a reason we use functions?
To test a condition for true and false. (WRONG)
For Questions 1-4, consider the following code: def mystery(w): if (w.upper() == w.lower()): return "TRUE" else: return "FALSE" What is output by the following line of code? print(mystery(")(Ƽ$"))
True
Consider the following code: def volume(x, y, z):return x * y * z#MAINv = 0length = int(input("Length: "))width = int(input("Width: "))height = int(input("Height: "))v = volume(length, width, height)print("Volume:" + str(v)) What is the output if the user enters 3, 7, 2?
Volume: 42
Functions go _____________ the main program in the source code file.
above
Consider the following function: def avg(a, b, c = 0): if (c ==0): print((a + b)/2) else: print((a + b +c)/3) Which of the following calls is NOT correct?
avg(99, 75, 86) WRONG!
Consider the following program: def sillyString(w): return w.replace( "a", "oo") #MAIN fruit = input ("Enter a fruit: ") print(sillyString(fruit)) What is output if the user enters "banana"?
boonoonoo
Which of the following parameters is optional? sample(a, b, c, d=7) (WRONG)
c
We ____________ a functions when we type its name.
call
Which of the following parameters is optional? sample(a, b, c, d = 0)
d
The __________ keyword creates the function.
def
The __________ keyword is used to create a function.
def
7.4 Code Practice: Question 2
def GPAcalc(g, w): if g == "a" or g == "A": return 4 + w elif g == "B" or g == "b": return 3 + w elif g == "C" or g == "c": return 2 + w elif g == "D" or g == "d": return 1 + w elif g == "F" or g == "f": return 0 + w else: return "Invalid" grade = input("Enter your Letter Grade: ") weight = int(input("Is it weighted? (1 = yes, 0 = no)")) gpa = GPAcalc(grade, weight) print("Your GPA score is: " + str(gpa))
7.5 Code Practice
def GPAcalc(g, w): if g == "a" or g == "A": return 4 + w elif g == "b" or g == "B": return 3 + w elif g == "c" or g == "C": return 2 + w elif g == "d" or g == "D": return 1 + w elif g == "f" or g == "F": return 0 + w else: return "Invalid" def GPAAV(S, C): return S/C Sum = 0 Classes = int(input("How many Classes are you taking? ")) for i in range(0, Classes): Grade = (input("Enter your Letter Grade: ")) Weight = int(input("Is it weighted? (1= yes) ")) gpa = GPAcalc(Grade, Weight) print("Your GPA score is: " + str(gpa)) Sum = Sum + gpa print("Your weighted GPA is a " + str(GPAAV(Sum, Classes)) + ".")
7.4 Code Practice: Question 1
def GPAcalc(letterG): if "a" in letterG.lower(): return 4 elif "b" in letterG.lower(): return 3 elif "c" in letterG.lower(): return 2 elif "d" in letterG.lower(): return 1 elif "f" in letterG.lower(): return 0 else: return("Invalid") letterGrade = input("Enter your Letter Grade: ") grade = GPAcalc(letterGrade) print("Your GPA score is: " + str(grade))
7.2 Code Practice
def ilovepython(): for i in range(3): print("I love Python") ilovepython()
Assignment 7: Calendar (89%)
def leap_year(y): if y % 4 == 0: return 1 else: return 0 def number_of_days(m,y): if m == 2: return 28 + leap_year(y) elif m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m ==10 or m == 12: return 31 elif m == 4 or m == 6 or m == 9 or m == 11: return 30 def days(m,d): if m == 1: return 0 + d if m == 2: return 31 + d if m == 3: return 59 + d if m == 4: return 90 + d if m == 5: return 120 + d if m == 6: return 151 + d if m == 7: return 181 + d if m == 8: return 212 + d if m == 9: return 243 + d if m == 10: return 273 + d if m == 11: return 304 + d if m == 12: return 334 + d def days_left(d,m,y): if days(m,d) <= 60: return 365 - days(m,d) + leap_year(y) else: return 365 - days(m,d) print("Please enter a date") day=int(input("Day: ")) month=int(input("Month: ")) year=int(input("Year: ")) choice=int(input("Menu:\n1) Calculate the number of days in the given month.\n2) Calculate the number of days left in the given year.\n")) if choice == 1: print(number_of_days(month, year)) if choice == 2: print(days_left(day,month,year))
Which of the following is NOT an example of a Python built-in function?
for
To create the body of a function, we ____________ the code.
indent
Functions are used to ________________________. (WRONG)
input data into a program
The ___________ go above the ___________ in the source code file. (WRONG)
main program, functions
Consider the following Python function definition: def mult(a, b = 1, c = 1):print(a * b * c) Which of the following calls is NOT correct?
mult(9, 15, 6, 7)
A variable used to send information to a function is called a ___________.
parameter
A variable used to send information to a subprogram is called a _______________.
parameter