functions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| functions [2023/11/03 06:27] – appledog | functions [2024/10/20 07:45] (current) – appledog | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| * Today I will do a lesson on functions. | * Today I will do a lesson on functions. | ||
| + | |||
| + | Note: This lesson might even be skipped, given that pyhang does not require functions. | ||
| == Functions | == Functions | ||
| Line 9: | Line 11: | ||
| * How to make a function (see examples below) | * How to make a function (see examples below) | ||
| - | == Code Example | + | |
| - | * Here is an example: | + | == Part 1: What's a function? Making functions |
| + | === Start Code | ||
| + | Show this to the students. Does it work? What do we need to do? | ||
| < | < | ||
| - | def main(): | + | x = 5 |
| - | | + | y = 3 |
| - | y = 12 | + | |
| - | z = add(x, y) | + | |
| - | print(z) | + | |
| - | def add(a, b): | + | a = add(x, y) |
| - | | + | s = sub(x. y) |
| - | | + | m = mul(x, y) |
| + | d = div(x, y) | ||
| - | if __name__ == " | + | print(a, s, m, d) |
| - | main() | + | |
| </ | </ | ||
| - | * There are three parts. | + | === Mid Code |
| - | ** One, "def main()" | + | Writing |
| - | ** Two, "def add(a, b)" creates a function with two arguments, or, parameters. | + | |
| - | ** Three, "if __name__ | + | |
| - | So you see, you can use the add() function to add numbers. | + | === End Code |
| + | adding idiv(), irem() | ||
| - | == Homework | ||
| - | * Classwork/ | ||
| - | ** Write a function to multiply two numbers. | ||
| - | ** Write a function to divide two numbers. | ||
| - | ** Write a function to subtract two numbers. | ||
| - | ** Test the program! //(How do you test? use print() to show the answer.)// | ||
| - | === Advanced | + | == Part 2: Refactoring a rot13 program |
| - | Is this too easy? Here are some more challenging questions: | + | === Start Code |
| + | We start here, and the journey is to refine the code and learn about functions as we move towards the end code. | ||
| - | * Write a function that returns a random number. | + | < |
| - | * Write a function that returns | + | a = " |
| - | or maybe, | + | for c in a: |
| - | * Write a function that prints | + | i = ord(c) |
| + | |||
| + | # upper case | ||
| + | if i >= 65 and i <= 90: | ||
| + | i = i + 13 | ||
| + | if i > 90: | ||
| + | i = i - 26 | ||
| + | |||
| + | # lower case | ||
| + | if i >= 96 and i <= 122: | ||
| + | i = i + 13 | ||
| + | if i > 122: | ||
| + | i = i - 26 | ||
| + | |||
| + | print(b, end='' | ||
| + | </ | ||
| + | |||
| + | |||
| + | === End Code | ||
| + | < | ||
| + | def main(): | ||
| + | | ||
| + | |||
| + | while a != "end": | ||
| + | print(rot13(a)) | ||
| + | |||
| + | print("" | ||
| + | a = input(" | ||
| + | |||
| + | def rot13(a): | ||
| + | r = "" | ||
| + | for c in a: | ||
| + | r += rot13_letter(c) | ||
| + | |||
| + | return r | ||
| + | |||
| + | def rot13_letter(c): | ||
| + | i = ord(c) | ||
| + | |||
| + | # capital letters | ||
| + | if is_letter(c): | ||
| + | i = i + 13 | ||
| + | if not is_letter(chr(i)): | ||
| + | i = i - 26 | ||
| + | |||
| + | return | ||
| + | |||
| + | def is_letter(c): | ||
| + | i = ord(c) | ||
| + | if i >=65 and i <= 90: | ||
| + | return True | ||
| + | |||
| + | elif i >= 96 and i <= 122: | ||
| + | return True | ||
| - | < | ||
| - | def isEven(a): | ||
| - | x = a // 2 #NOTE: / always returns a float. // always returns the lowest integer! | ||
| - | if a == x: | ||
| - | return " | ||
| else: | else: | ||
| - | return | + | return |
| + | |||
| + | if __name__ == ' | ||
| + | main() | ||
| + | | ||
| </ | </ | ||
| - | There are many, many ways to do this. The students should be able to think of at least this: | + | == Part 3: 3n+1 example |
| + | Now the students must do things on their own. | ||
| - | < | + | === Start Code |
| - | def isEven(a): | + | < |
| - | while a >= 2: | + | |
| - | a = a - 2 | + | n = 10 |
| - | | + | |
| - | if a == 0: | + | while n > 1: |
| - | | + | |
| + | if n % 2 == 0: | ||
| + | | ||
| + | n = n // 2 | ||
| else: | else: | ||
| - | print(" | + | |
| + | n = 3 * n + 1 | ||
| + | |||
| + | | ||
| + | |||
| + | if __name__ == ' | ||
| + | main() | ||
| + | </ | ||
| + | |||
| + | === End Code | ||
| + | < | ||
| + | def main(): | ||
| + | n = input("Please enter a number: | ||
| + | n = int(n) | ||
| + | c = 0 | ||
| + | |||
| + | print(" | ||
| + | |||
| + | while n > 1: | ||
| + | print(', | ||
| + | c = c + 1 | ||
| + | if c % 10 == 0: | ||
| + | print("" | ||
| + | |||
| + | if n % 2 == 0: | ||
| + | n = n // 2 # Even | ||
| + | else: | ||
| + | n = 3 * n + 1 # Odd | ||
| + | |||
| + | print(str(n), | ||
| + | |||
| + | print(" | ||
| + | print(f" | ||
| + | |||
| + | if __name__ == ' | ||
| + | main() | ||
| </ | </ | ||
| - | If this challenge is too easy, they can always write a function that determines if a number is prime. | ||
functions.1698992852.txt.gz · Last modified: 2023/11/03 06:27 by appledog
