Functions and While Loops Study Guide 1. Codecademy - Intro to Functions 2. Exercise 3 3. Functions Practice 4. Intro to While Loops/Guessing Game 5. Practice Questions (Solutions at end): 1. What is printed to the screen? def function3(x,y): if x<y: print (x) else: print (y) function3(4,6) a. b. c. d. 4 6 None Error 2. What is printed to the screen? def function2(x,y): print(x*y) print(function2(3,4)) a. 3*4 b. 12 c. 12 None d. Error 3. What is printed to the screen? def f1(x,y,z): if x==y: return z elif x==z: return y else: return x print(f1(3,4,4)) a. b. c. d. 3 4 Error None 4. What is printed to the screen? def f2(name,number): print(name,number) print(f2(“Phillips”,1)) a. Error b. Phillips1 c. Phillips1 None d. Phillips 1 5. What is printed to the screen? def f2(name,word): print(word[0:1]+name+word[2:]) f2(“JON”,”HELLO”) a. b. c. d. Error HJONELLO HEJONLLO HJONLLO 6. What is returned from the function? def f2(s,r): print(s*r) return s return r f2(3,2) a. b. c. d. 6 3 2 3,2 7. Which function returns 6 when f3(3) is called? a. def f3(x): if x>4: return x+2 else: return x*2+1 c. def f3(x): if x>4: return x else: return x**2 b. def f3(x): if x<4: return x else: return x*2 d. def f3(x): if x>4: return x else: return x*2 8. What is printed to the screen? x=10 while x>8: print(x) x=x-1 a. 10 9 8 b. 10 9 8 7 c. 10 9 d. An infinite number of 10s 9. What is printed to the screen? while x<3: print(x) x=x+1 a. An infinite number of 0s b. Error c. 0 1 2 d. 0 1 2 3 10. What is printed to the screen? while True: x=2 if x==5: break else: print(x) x=x+1 a. An infinite number of 2s b. 2 3 4 c. 2 3 4 5 d. Error 11. What is printed to the screen? x=”Play” while True: if x==”Play”: print (“Play Again”) x=”quit” elif x==”quit”: break a. Nothing b. An infinite number of “Play Again”s c. Play Again d. Play Again Play Again 12. What function returns True if the argument is odd? a. b. def f1(x): if x//2=1: return True c. def f1(x): if x%2=1: return True d. def f1(x): if x%2==1: return True 13. What is returned from the function? def function3(x,y): if x<y: print (x) else: print (y) function3(4,6) a. b. c. d. 14. What is 4 6 None Error printed to the screen? def a_function(a): print(a*2) a_function(3,2) a. b. c. d. 6 a*2 Error Nothing def f1(x): if not x%2==1: return True 15. What is printed to the screen? def my_function(): print(“What’s Up”) a. What’s Up b. Error c. Nothing Solutions: 1. a No tricks here - the function just checks if 4 is less than 6 and prints 4. 2. c This function prints 12 inside the body of the function. The print(function2(3,4)) line also tries to print something but because nothing is returned it prints “None”. 3. a When this function is called, x=3, y=4, z=4. Since x is not equal to y and x is not equal to z it returns the “else” part of the code - x (which equals 3) 4. C Similar to #2. This function prints inside the body of the function but does not return anything. Therefore it will also print “None” from the print(f2(“Phillips”,1)) line of code. 5. D word [0:1] just gets the first letter of word (H) word [2:] gets the letters in word starting with the third letter (L) and going to the end of the word. All combined it makes H + JON + LLO = HJONLLO 6. B The print(s*r) statement doesn’t return anything so that can’t be the answer The code never reaches the return s statement because as soon as it gets to return r, it returns the value of r (3) and exits the function. 7. D X has a value of 3 in the function. Looking at the different options carefully will show that d is the only option that returns a value of 6. 8. C The first time through the loop, x =10 and is printed to the screen. Then x is decreased by 1 to become 9. The next time through the loop x=9 and is printed to the screen. Then x is decreased by 1 to become 8. The while loop sees that x is no longer less than 8 so the loop stops. 9. B This gives an error because we never define a starting value for x 10. A An infinite number of 2s are printed because the value of x is reset to 2 each time through the loop. 11. C The first time through the loop, x==”Play” is true so it prints “Play Again”. Then x becomes “quit”. The next time through the loop x==”quit” is True so it breaks the loop and doesn’t print anything else. 12. C If a number divided by 2 gives a remainder of 1 it is odd. The % sign gives the remainder of a division. If you are checking if something is equal to something else you need the double equals sign (==) so b cannot be correct. 13. C There is no return statement so nothing is returned. 14. C The function is called with 2 arguments (3,2) but only has one parameter (a) so it causes an error. 15. C The function is never called so it doesn’t print anything