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
functions [2024/07/29 00:09] appledogfunctions [2024/10/20 07:45] (current) appledog
Line 11: 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> <Code:Python>
-def main(): +x = 5 
-    x = 5 +y = 3
-    y = 12 +
-    z = add(x, y) +
-    print(z)    +
  
-def add(ab): +a = add(xy
-    a + b +sub(x. y) 
-    return c+m = mul(x, y) 
 +d = div(x, y)
  
-if __name__ == "__main__": +print(a, s, m, d)
-    main()+
 </Code> </Code>
  
-* There are three parts. +=== Mid Code 
-** One, "def main()" creates the main() function. +Writing the four functions
-** 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 seeyou can use the add() function to add numbers.+=== End Code 
 +adding idiv()irem()
  
-== Homework 
-* 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 +== 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. +<Code:Python> 
-* Write a function that returns random fruit name.+= "hello"
  
-or maybe+for c in a: 
-* Write function that prints "evenor "oddif the number is even or odd.+    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(bend='') 
 +</Code>     
 + 
 + 
 +=== End Code 
 +<Code:Python> 
 +def main(): 
 +    = input("Enter the code: "
 + 
 +    while a != "end"
 +        print(rot13(a)) 
 + 
 +        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
  
-<Code:Python|Example answer!> 
-def isEven(a): 
-    x = a // 2      #NOTE: / always returns a float. // always returns the lowest integer! 
-    if a == x: 
-        return "even" 
     else:     else:
-        return "odd"+        return False 
 + 
 +if __name__ == '__main__': 
 +    main() 
 +    
 </Code> </Code>
  
-There are many, many ways to do thisThe students should be able to think of at least this:+== Part 3: 3n+1 example 
 +Now the students must do things on their own.
  
-<Code:Python|Example answer 2!+=== Start Code 
-def isEven(a): +<Code:Python> 
-    while >= 2+ 
-        a = a - 2 +n = 10 
-         + 
-    if == 0: +while 1
-        print("even")+ 
 +    if n % 2 == 0: 
 +        # Even 
 +        n = n // 2
     else:     else:
-        print("odd")+        # Odd 
 +        n = 3 * n + 1 
 + 
 +        print(str(n)) 
 + 
 +if __name__ == '__main__': 
 +    main() 
 +</Code> 
 + 
 +=== End Code 
 +<Code:Python> 
 +def main(): 
 +    n = input("Please enter a number: "
 +    n = int(n) 
 +    c = 0 
 + 
 +    print("Start: " + str(n), end = ''
 + 
 +    while n > 1: 
 +        print(', ', end = ''
 +        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), 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.1722211759.txt.gz · Last modified: 2024/07/29 00:09 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki