User Tools

Site Tools


functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
functions [2023/11/03 06:26] appledogfunctions [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?
 +
 +<Code:Python>
 +x = 5
 +y = 3
 +
 +a = add(x, y)
 +s = sub(x. y)
 +m = mul(x, y)
 +d = div(x, y)
 +
 +print(a, s, m, d)
 +</Code>
 +
 +=== Mid Code
 +Writing the four functions
 +
 +=== End Code
 +adding idiv(), irem()
 +
 +
 +== Part 2: Refactoring a rot13 program
 +=== Start Code
 +We start here, and the journey is to refine the code and learn about functions as we move towards the end code.
 +
 +<Code:Python>
 +a = "hello"
 +
 +for c in a:
 +    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='')
 +</Code>    
 +
 +
 +=== End Code
 <Code:Python> <Code:Python>
 def main(): def main():
-    +    input("Enter the code: ")
-    y = 12 +
-    z = add(x, y) +
-    print(z   +
  
-def add(a, b)+    while != "end"
-    c = + b +        print(rot13(a))
-    return c+
  
-if __name__ == "__main__":+        print(""
 +        a = input("Enter the next code: ") 
 + 
 +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  chr(i) 
 + 
 +def is_letter(c): 
 +    i = ord(c) 
 +    if i >=65 and i <= 90: 
 +        return True 
 + 
 +    elif i >= 96 and i <= 122: 
 +        return True 
 + 
 +    else: 
 +        return False 
 + 
 +if __name__ == '__main__':
     main()     main()
 +    
 </Code> </Code>
  
-* There are three parts. +== Part 3: 3n+1 example 
-** One, "def main()" creates the main() function. +Now the students must do things on their own.
-** Two, "def add(a, b)" creates a function with two arguments, or, parameters. +
-** Three, "if __name__ == main...." means, to run the "main" function first.+
  
-So you see, you can use the add() function to add numbers.+=== Start Code 
 +<Code:Python>
  
-== Homework +10
-* Classwork/Hommework: Students must:  +
-** 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 +while n > 1:
-Is this too easy? Here are some more challenging questions:+
  
-* Write a function that prints "even" or "odd" if the number is even or odd. +    if n % 2 == 0
-<Code:Python|Example answer!> +        # Even 
-def isEven(a): +        // 2
-    // 2      #NOTE: / always returns a float. // always returns the lowest integer! +
-    if a == x: +
-        return "even"+
     else:     else:
-        return "odd"+        # Odd 
 +        n = 3 * n + 1 
 + 
 +        print(str(n)) 
 + 
 +if __name__ == '__main__': 
 +    main()
 </Code> </Code>
  
-There are many, many ways to do this. The students should be able to think of at least this:+=== End Code 
 +<Code:Python> 
 +def main(): 
 +    n = input("Please enter a number: ") 
 +    n = int(n) 
 +    c = 0
  
-<Code:Python|Example answer 2!> +    print("Start" + str(n), end = ''
-def isEven(a): + 
-    while >= 2+    while 1
-        a - 2 +        print(', ', end '') 
-         +        c = c + 1 
-    if == 0: +        if c % 10 == 0: 
-        print("even") +            print(""
-    else: + 
-        print("odd")+        if n % 2 == 0: 
 +            n = n // 2      # Even 
 +        else: 
 +            n = 3 * n + 1   # Odd 
 + 
 +        print(str(n), end = ''
 + 
 +    print("."
 +    print(f"There were {c} numbers."
 + 
 +if __name__ == '__main__': 
 +    main()
 </Code> </Code>
  
-If this challenge is too easy, they can always write a function that determines if a number is prime. 
  
functions.1698992812.txt.gz · Last modified: 2023/11/03 06:26 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki