230 likes | 373 Views
Using functions:. Remember: after we use a function, what remains is what is returned from the function def add2( x,y ): return(x + y) def add( x,y ): return(add2( x,y ) + add2( x,y )) print(add(7,3)). Using functions:. def add2( x,y ): return(x + y) def div( x,y,z ):
E N D
Using functions: • Remember: after we use a function, what remains is what is returnedfrom the function def add2(x,y): return(x + y) def add(x,y): return(add2(x,y) + add2(x,y)) print(add(7,3))
Using functions: def add2(x,y): return(x + y) def div(x,y,z): return(add2(x,y) / z) print(div(7,3,2))
Using functions: def add2(x,y): return(x + y) def div(x,z): return(add2(x,3) / z) print(div(7,2))
Using functions: def add2(x,y): return(x + y) def div(y,x): return(add2(y,3) / x) print(div(7,2))
Using functions: def add2(x,y): return(x + y) def add3(y,x): return(add2(y,3) + add2(11,x)) print(add3(7,2))
def f1(par1, par2): return(par2 - par1) print(f1(2,4)) #2 def f2(x1,x2): return(x1**2 + x2) print(f2(3,6)) #15 def f3(p1,p2): return(f2(p1,p2) + f1(p1,p2)) print(f3(3,2)) 10 def f4(p1,p2): return(f2(p2,p2) - f1(p1,p1)) print(f4(4,2)) 6 def f5(q1,q2): return(f2(q2,q1)) print(f5(17,5)) 42 def f6(par1,par2): return( 3 + f1(par1, 17+par1)) print(f6(4,26)) 20
Given the function def Squr(par1): return(par1 ** 2) def dbl(par2): return(par2 + par2) What does this get us? def Func1(p1,p2): return(Squr(p1) - Squr(p2)) print(Func1(4,3)) >>7 def Func2(p1,p2,p3): return(Squr(p1) * Func1(p2,p3)) print(Func2(2,3,2)) >>20 def Func3(p1,p2): return(dbl(Squr(p1))) print(Func3(4)) >>AACH CRASH BURN def Func4(p1,p2): return(dbl(Squr(p2)+ Squr(p2)+3)) print(Func4(2,4)) >> 70 def Func5(p1): return(dbl(dbl(dbl(p1)))) print(Func5(4)) >>32
If /else (branching) 32 if x > 0 _ f(x) = x 0 otherwise def f(x): if x > 0: return (3**2/x) else: return (0) f(3) # this equals? f(0) # this equals? f(-2) # this equals?
x3 + 2x if x > 2 f(x) = -x3 + 2x if x < 0 -1 otherwise If /else (branching) def f(x): if x > 2: return (x ** 3 + 2 * x) elif x < 0: return(-x ** 3 + 2 * x) else: return (-1) f(3) # this equals? f(0) # this equals? f(-2) # this equals?
Example def f(x): if x > 10: return (x+9) elifx < 7: return (x + 4) else: return(0) print(f(12)) # what is printed? print(f(6)) # what is printed? print(f(8)) # what is printed? print(f(7)) # what is printed?
Example def f(x): if x > 5: return (x*4) elif x > 4: return(x * 6) elif x == 4: return (x*3) else: return(x*2) print(f(5)) # what is printed? print(f(4)) # what is printed? print(f(3)) # what is printed?
Example def f(x): if x != 10: return (x * 2) else: return (x ** 2) print(f(6)) print(f(12)) print(f(10))
Example def f(x): if x < 10: return (x+9) elif x < 5: return (x + 4) elif x < 0: return (x) else: return(0) print(f(-1)) ?
and def q(x): if (x>5) and (x < 10): return("just enough") elif (x >= 10) and (x < 15): return("too much") else: return("no idea") print(q(12)) • What does and do? • What type is returned from this function?
diff? defq2(x): if (x>6) or (x < 5): return("just enough") elif (x > 15) or (x < 20): return("too much") else: return("no idea") print(q2(7)) print(q2(13)) defq1(x): if (x>6) and (x < 5): return("just enough") elif (x > 15) and (x < 20): return("too much") else: return("no idea") print(q1(7)) print(q1(13))
What happens? def ReturnSomething(value): if value = 1: return “glub” else: return “blug” print (ReturnSomething(1))
Adding Strings: defaddstrings(par1): return(par1 + "ubba") print (addstrings("gub")) def addmore(par1): return(addstrings(par1)+addstrings(par1)) print(addmore("hab"))
Printing inside my function: • What if I want to see the string that was input into the function? defaddstrings(par1): print(par1 + “came in”) return(par1 + "ubba") print (addstrings("gub")) Now we’re adding what’s inside par1 to “came in” and that is what gets printed by the function before we leave the function.
Solution def f_to_c(ftemp): print("The temp before conversion is” + str(ftemp)) return((ftemp - 32 )/ 1.8) print (f_to_c(68)) print (f_to_c(22)) • Note: • ftemp is not in quotes. • When it is not in quotes, we’re talking about what’s inside of ftemp and not the word ftemp • what is inside of ftemp is an integer. • We can’t add integers to strings • str(ftemp) • takes the number inside of the parameter ftemp and converts it to a string
defgetverb(x): if x == 17: return("am") elif x == 24: return("was") elif x == 32: return("love") else: return("ran over") print(getverb(27)) defmakeit(x,y,z): return(getname(z) + getpron(y) + getverb(x)) print(makeit(17,3,7)) defgetname(x): if x == 3: return("John") elif x == 13: return("Joe") elif x == 7: return("Sam") else: return("Bob") print(getname(13)) defgetpron(x): if x == 2: return("you") elif x == 3: return("I") elif x == 4: return("she") elif x == 5: return("he") else: return("it") print(getpron(5))
#input : 3 integers, x, y and z #Output: a string # “Yes x is divisible by both y and z” or # “No, x is not evenly divisible by y and z” # “x is not in range” #Function name: isDivisible #Calculations: check if x is greater than 0 and less than 100 and is evenly #divisible by both y and z def isDivisible(x, y,z) if (x > 0)and (x < 100): if ((x%y) == 0) and ((x % z) == 0): return (“Yes “+str(x)+” is divisible by both “+str(y)+” and “+str(z)) else: return (“No, “+str(x)+” isn’t evenly divisible by “+str(y)+” and “+str(z)) else: return(str(x ) + “ is not in range”) print(isDivisible(15,5,3)) print(isDivisible(150,5,3)) Now what if x is 250 or -1?
Same? • def g(x): • if (x > 5): • if (x < 10): • return("just enough") • elif (x < 15): • return("too much") • else: • return("no idea") defg(x): if (x>5) and (x < 10): return("just enough") elif (x > 5) and (x < 15): return("too much") else: return("no idea") • print (g(12)) • What about: • print (g(17))
Loan Qualifier We want to write a function that tells someone whether they qualify for a loan. • If a person makes more than 35,000 and they’ve been employed for at least 2 years, • they qualify. • If they make over 35,000, but haven’t been employed for at least 2 years, • They should get a message saying how long they need to wait before they can get the loan • (e.g., if they’ve only been employed for 1.2 years, the program should tell them to come back in .8 years) • If they don’t make 35,000, but have been employed for over 2 years, • They should get a message telling them the minimum salary requirement • If they don’t make 35,000 and they haven’t been employed for 2 years, • they don’t qualify. Using Nested If (ifs inside of ifs) can you write this?