1 / 28

and

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?. Logical Operators. diff?. def q2(x):

Télécharger la présentation

and

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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?

  2. Logical Operators

  3. diff? def q2(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)) def q1(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))

  4. What happens? def ReturnSomething(value): if value = 1: return “glub” else: return “blug” print (ReturnSomething(1))

  5. Adding Strings: defaddstrings(par1): return(par1 + "ubba") print (addstrings("gub")) def addmore(par1): return(addstrings(par1)+addstrings(par1)) print(addmore("hab"))

  6. 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")) We want to print what’s inside par1, not “par1”

  7. 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.

  8. Printing inside a function deff_to_c(ftemp): print("The temp before conversion is ftemp") return((ftemp - 32 )/ 1.8) print (f_to_c(68)) print (f_to_c(22)) • Is this what we wanted? deff_to_c(ftemp): print("The temp before conversion is” + ftemp) return((ftemp - 32 )/ 1.8) print (f_to_c(68)) print (f_to_c(22))

  9. 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

  10. Input function • NOT the same as an input parameter!!!! def f(x): return(x + input(“What do you want to say?”)) f(“You said ”) • What type does the input function return? def f(x): return(x + input(“What do you want to add?”)) f(3) • Instead: def f(x): return(x + int( input(“What do you want to add?”))) f(3)

  11. 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))

  12. New Function • 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: • Two parts: • check if x is between 0 and 100 • Check if x is evenly divisible by both y and z

  13. #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)) and ((x%y) == 0) and (x % z) == 0): #ugh! Long and hard to read return (“Yes “+str(x)+” is divisible by both “+str(y)+” and “+str(z)) else: return (“No, “+str(x)+” is not evenly divisible by “+str(y)+” and “+str(z)) print(isDivisible(15,5,3)) print(isDivisible(150,5,3)) Is this what we want ? Will it always work?

  14. #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)) and ((x%y) == 0) and (x % z) == 0): return (“Yes “+str(x)+” is divisible by both “+str(y)+” and “+str(z)) elif((x > 0)and (x < 100)) : return (“No, “+str(x)+” is not 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)) Is this what we want ? Will it always work?

  15. #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?

  16. Same? def g(x): if (x > 5): if (x < 10): return("just enough") elif (x < 15): return("too much") else: return("no idea") def g(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))

  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?

  18. LoanQualifier def loanqualifier(sal,yrs): if (sal > 35000): if (yrs >= 2): return("Congratulations! You qualify!") else: return("You will qualify in " + str(round(2-yrs) ,2)+ " years.") else: if (yrs>=2): return("You need to make at least 35000 to qualify for a loan") else: return("I'm sorry, you don't qualify.") #Note the test cases – we’re testing all outputs to make sure they work print (loanqualifier(40000,4)) print (loanqualifier(40000,1.2)) print (loanqualifier(20000,4)) print (loanqualifier(20000,1.2))

  19. Quick function: • Write a function that checks to see if a number is even or odd. • What TYPE does this return?

  20. def ismultof3(x): if ((x%3) == 0): return(True) else: return(False) When python executes the following statement, what is the result? (x%3)==0 def ismultof3(x): return((x%3) == 0) def func2(x): if (ismultof3(x)): # Can we see why specifying what type # is returned from a function is critical?!? return(str(x) + " is a multiple of 3") else: return(str(x) + " is not a multiple of 3")

  21. Returning to Boolean Values: • Quadratic Equation: • x2 -3x – 4 = 0 • Is this true for 1? 2? 3? 4? • Can you write a function that returns the answer to this question? • Hint: the function needs to return a boolean value. • What is the input? • How do you check for the output?

  22. Function to represent this: #Name: eqcheck #Calculation: Determines if input value (x) will solve #the problem: # x2 -3x – 4 = 0 #Input: x: a number #Output: a boolean value def eqcheck(x): return (x**2 –3*x – 4) == 0 What is returned? print(eqcheck(3)) What is returned? print(eqcheck(4))

  23. Remember? defSqur(par1): return(par1 ** 2) defdbl(par2): return(par2 + par2) def Func4(p1,p2): return(dbl(Squr(p2)+ Squr(p2)+3)) print(Func4(2,4)) def Func5(p1): return(dbl(dbl(dbl(p1)))) print(Func5(4))

  24. What gets printed out? def f(n): if (n == 0): return (" ") else: print(“blug ") return f(n-1) f(3)

  25. What about? deff(x): if (x == 0): return x else: return(x + f(x-1)) print(f(4)) deff2(x): if (x == 1): return (str(x)) else: return(str(x) + f2(x-1)) print(f2(4))

  26. Recursion • Recursion is when a function is defined in terms of itself (it calls itself). • Def: A recursivedefinitionis one that defines something in terms of itself (that is, recursively) • Recursion is, in essence, making a function happen again and again without our having to call it (convenient, huh?) #This is recursion def recurse(): return(recurse()) #This isn’t def nonrecurse(): return(“nope”)

  27. Try: def f3(x): if (x == 1): return x else: return(x + f3(x-2)) print(f3(4,0)) def f(x): return(x + f(x-1)) print(f(4)) def f2(x): if (x == 1): return x else: return(x + f2(x+1)) print(f2(4))

  28. How about: def f(x): if x == 100: return(“none") else: if (x**2 - 3 *x - 4) == 0: print(str(x) + " solves the equation ") return(f(x+1)) print(f(-100))

More Related