#!/usr/bin/env python # coding: utf-8 # #### Before you start working on these exercises, make sure you've watched the videos, done the reading, and taken the quiz! # # General Resources: # * [Main PY4E Website](https://www.py4e.com) # * [Full Textbook PDF](http://do1.dr-chuck.com/pythonlearn/EN_us/pythonlearn.pdf) # * [Full Youtube Playlist](https://www.youtube.com/playlist?list=PLlRFEj9H3Oj7Bp8-DfGpfAfDBiblRfl5p) # # Lesson 4 Resources: # * [Lesson Page](https://www.py4e.com/lessons/functions) # * [Video 4.1](https://www.youtube.com/watch?v=5Kzw-0-DQAk&list=PLlRFEj9H3Oj7Bp8-DfGpfAfDBiblRfl5p&index=19) # * [Video 4.2](https://www.youtube.com/watch?v=AJVNYRqn8kM&list=PLlRFEj9H3Oj7Bp8-DfGpfAfDBiblRfl5p&index=20) # * [Lesson Slides](https://www.py4e.com/lectures3/Pythonlearn-04-Functions.pptx) # * [Textbook Chapter 4](https://www.py4e.com/html3/04-functions) # * [Link to Quiz (must be logged in)](https://www.py4e.com/lessons_launch/py4e_04_def_quiz) # * If you're feeling stuck on exercise 4.6, worked solutions are available on the lesson page and Youtube # In[ ]: # Make sure to run this cell to load the autograder! from grader import check_exercise # **Exercise 1: Run the program on your system and see what numbers you get. # Run the program more than once and see what numbers you get.** # In[ ]: ### Run This Code ### import random for i in range(10): x = random.random() print(x) Answer: The result of this program was ... # **Exercise 2: Move the last line of this program to the top, so the # function call appears before the definitions. Run the program and see # what error message you get.** # In[ ]: ### Run This Code ### repeat_lyrics() def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') def repeat_lyrics(): print_lyrics() print_lyrics() Answer: The result of this program was ... # **Exercise 3: Move the function call back to the bottom and move the # definition of `print_lyrics` after the definition of `repeat_lyrics`. # What happens when you run this program?** # In[ ]: repeat_lyrics = None # Resets functions for Exercise 3 print_lyrics = None # Resets functions for Exercise 3 ### Run This Code ### def repeat_lyrics(): print_lyrics() print_lyrics() def print_lyrics(): print("I'm a lumberjack, and I'm okay.") print('I sleep all night and I work all day.') repeat_lyrics() Answer: The result of this program was ... # **Exercise 4: What is the purpose of the "def" keyword in Python?** # # a\) It is slang that means "the following code is really cool"\ # b) It indicates the start of a function\ # c) It indicates that the following indented section of code is to be # stored for later\ # d) b and c are both true\ # e) None of the above # In[ ]: exercise = 'Exercise 4.4' ### Begin Answer Here ### answer = ### End Answer Here ### print(repr(answer)) # In[ ]: # Make sure to save your notebook before checking! check_exercise('Exercise 4.4') # **Exercise 5: What will the following Python program print out?** # # ~~~~ {.python} # def fred(): # print("Zap") # # def jane(): # print("ABC") # # jane() # fred() # jane() # ~~~~ # # a\) Zap ABC jane fred jane\ # b) Zap ABC Zap\ # c) ABC Zap jane\ # d) ABC Zap ABC\ # e) Zap Zap Zap # In[ ]: exercise = 'Exercise 4.5' ### Begin Answer Here ### answer = ### End Answer Here ### print(repr(answer)) # In[ ]: # Make sure to save your notebook before checking! check_exercise('Exercise 4.5') # **Exercise 6: Rewrite your pay computation with time-and-a-half for # overtime and create a function called `computepay` which # takes two parameters (`hours` and `rate`), and returns a float.** # # ~~~~ # Enter Hours: 45 # Enter Rate: 10 # Pay: 475.0 # ~~~~ # In[ ]: exercise = 'Exercise 4.6' ### Start Code Here ### ### End Code Here ### # In[ ]: # Make sure to save your notebook before checking! check_exercise('Exercise 4.6') # **Exercise 7: Rewrite the grade program from the previous chapter using a # function called `computegrade` that takes a score as its # parameter and returns a grade as a string.** # # ~~~~ # Score Grade # >= 0.9 A # >= 0.8 B # >= 0.7 C # >= 0.6 D # < 0.6 F # ~~~~ # # ~~~~ # Enter score: 0.95 # A # ~~~~ # # ~~~~ # Enter score: perfect # Bad score # ~~~~ # # ~~~~ # Enter score: 10.0 # Bad score # ~~~~ # # ~~~~ # Enter score: 0.75 # C # ~~~~ # # ~~~~ # Enter score: 0.5 # F # ~~~~ # # Run the program repeatedly to test the various different values for # input. # In[ ]: exercise = 'Exercise 4.7' ### Start Code Here ### ### End Code Here ### # In[ ]: # Make sure to save your notebook before checking! check_exercise('Exercise 4.7')