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