+{"cells":[{"cell_type":"markdown","metadata":{"id":"jNAI57ELh-I8"},"source":["# Functions\n","## Practice Problems"]},{"cell_type":"markdown","metadata":{"id":"QDChISArjYWi"},"source":["### 1. Following the function design recipe, define:\n"," * a function that converts Fahrenheit into Celsius\n"," * a function that converts kilometers into miles. (Assume there are 1.6 kilometers in a mile.)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PM5C4swdjarO"},"outputs":[],"source":["def fahrenheit_to_celsius(f):\n"," celsius = (f- 32) * 5/9\n","return Celsius\n"]},{"cell_type":"markdown","metadata":{"id":"dzPArgv6jfJ-"},"source":["### 2. What value is printed in the below code?"]},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":8,"status":"ok","timestamp":1667929905248,"user":{"displayName":"Kaylie Lau","userId":"01284785813595846851"},"user_tz":300},"id":"SunFsOXdjhBq","outputId":"9210bf31-d0db-4f4f-d96c-69a9f693b002"},"outputs":[],"source":["answer = 3\n","\n","def answer_to_everything():\n"," '''Return the answer to life, the universe, and everything'''\n"," answer = 42\n"," return answer\n"," \n","answer_to_everything()\n","\n","print(answer)"]},{"cell_type":"markdown","metadata":{"id":"_y9cC0sZuDAZ"},"source":["<details>\n"," <summary>Answer</summary>\n","\n"," Answer: 3. The answer variable in our function is locally scoped -- our program cannot access the value 42 from outside of the function, including when we called print(answer).\n","\n"," Although we called answer_to_everything(), we did not assign the output to a variable.\n","</details>"]},{"cell_type":"markdown","metadata":{"id":"57Tr7WP5jnZe"},"source":["### 3. Complete the examples in the docstring below, then write the body of the function. You can assume `num` is always >= 0."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"l3B2ojfCjqmq"},"outputs":[],"source":["def repeat(string, num):\n"," ''' Return string repeated num times.\n"," >>> repeat('yes', 4)\n"," 'yesyesyesyes'\n"," >>> repeat('no', 0)\n"," # your expected output here\n"," >>> repeat('yesnomaybe', 3)\n"," # your expected output here\n"," '''\n"," output = string*num\n"," return output"]},{"cell_type":"markdown","metadata":{"id":"1hXddQH1jtO5"},"source":["### 4. Complete the function below. You can assume that `number` will always be a string with 10 digits and that the first 3 digits will always be the area code."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hcACgOltjs9r"},"outputs":[],"source":["from pickle import TRUE\n","def is_416_number(number):\n"," ''' Check whether the number has a 416 area code.\n"," >>> is_416_number('(416)-555-5555')\n"," True\n"," >>> is_416_number('514 416 5555')\n"," False\n"," >>> is_416_number('4165554160')\n"," True\n"," '''\n"," # replace parens w/ nothing\n"," number = number.replace('(', '')\n"," number = number.replace(')', '')\n"," output = number[:3] == '416'\n"," return output"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyMhUBNX+UP2C+YDtVSxQKBK","collapsed_sections":[],"provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
0 commit comments