From cisc106 import * def f(x): if x < 10: return (x+9) elif x < 5: return (x + 4) elif x < 0: return (x) else: return(0) assertEqual(f(-1), ______)

Slides:



Advertisements
Similar presentations
Code: from cisc106 import * def myfunc(value1,value2): return (value1 + value2) assertEqual(myfunc(3,2),5) assertEqual(myfunc(7,4),11) assertEqual(myfunc(9,8),17)
Advertisements

Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Def f(x): if (x == 1): return x else: return(x + f(x-1)) assertEqual(f(4),______) def f2(x): if (x == 1): return str(x) else: return(str(x) + f2(x-1))
Def f(x): counter = 0 y = 0; while counter
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Section 5.3: Finding the Total Area Shaded Area = ab y = f(x) x y y = g(x) ab y x Shaded Area =
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Programming Training Main Points: - Python Statements - Problems with selections.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
PPT 2. Other things: functions  Can you have a function with no inputs?  Yes: def f( ): return (3 + 4 )  Can you have a function with no outputs? 
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
It’s time for Math Class Today we are going to be working on 2-digit by 2-digit multiplication. Yuk Math…I’m out of here!
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
PPT3. Quick function:  Write a function that checks to see if a number is even or not.  What TYPE does this return?
More While Loop Examples CS303E: Elements of Computers and Programming.
PPT 4. Variables: (assignment)  A variable: holds a value.  A space in RAM we give a name to  A lot like parameters, only we create them within the.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
CS101 Computer Programming I Chapter 4 Extra Examples.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
4-3: Riemann Sums & Definite Integrals
Python Conditionals chapter 5
1 Solve each: 1. 5x – 7 > 8x |x – 5| < 2 3. x 2 – 9 > 0 :
8.2 Operations With Matrices
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Python if.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Inverses are everywhere – when we think about reversing a process, we are thinking about the inverse.
Dictionaries Python.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Before Find the vertex and zeros and then graph Copyright © by Houghton Mifflin Company, Inc. All rights reserved.1.
Making Choices with if Statements
Module 3.
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Other things: functions
Section 5.4 Theorems About Definite Integrals
Javascript: variables and parameters
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Types, Truth, and Expressions (Part 2)
Practice with loops! What is the output of each function below?
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Introduction to Python
CS 1111 Introduction to Programming Spring 2019
To find the inverse of a function
Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3.
Area Between Two Curves
What is printed out? def f(x): print(x+3) return(x + 7) def g(k): return(f(k) + 2) print(g(1)) 4 10 def f2(x): return(x + 7) print(x+3) def g2(k): return(f2(k)
Finding the Total Area y= 2x +1 y= x Area = Area =
Nate Brunelle Today: Conditional Decision Statements
To find the inverse of a function
More While Loops.
Finding the Total Area y= 2x +1 y= x Area = Area =
Types, Truth, and Expressions
CSE 231 Lab 2.
Problem to solve Given four numbers, days, hours, minutes, and seconds, compute the total number of seconds that is equivalent to them. For example, 0.
Functions John R. Woodward.
Control 9 / 25 / 19.
Presentation transcript:

from cisc106 import * def f(x): if x < 10: return (x+9) elif x < 5: return (x + 4) elif x < 0: return (x) else: return(0) assertEqual(f(-1), ______)

def addstrings(par1): return(par1 + "ubba") assertEqual(addstrings("gub"),"gububba") def addmore(par1): return(addstrings(par1) +addstrings(par1)) assertEqual(addmore(“ha"), "_____________")

def dow(x): if (x == 1): return("Sunday") elif (x == 2): return("Monday") elif (x == 3): return("Tuesday") elif (x == 4): return("Wednesday") elif(x == 5): return("Thursday") elif (x == 6): return("Friday") elif (x == 7): return ("Saturday") else: return("Error") assertEqual(dow(3),"Tuesday") def makestr(x): if (dow(x) !="Error"): return("Today is " + dow(x)) else: return("Bad day") assertEqual(makestr(4),________) assertEqual(makestr(9),________) def makestr(x): if (dow(x)=="Error"): return("Bad day") else: return("Today is " + dow(x)) assertEqual(makestr(9),________) assertEqual(makestr(-1),________)

def q(x): if (x>5) and (x < 10): return("just enough") elif (x > 5) and (x < 15): return("too much") else: return("no idea") assertEqual(q(12), "___________")

def q(x): if (x>5) and (x < 10): return("just enough") elif (x > 5) and (x < 15): return("too much") else: return("no idea") assertEqual(q(7), "________") def q(x): if (x>5) or (x < 10): return("just enough") elif (x > 5) or (x < 15): return("too much") else: return("no idea") assertEqual(q(13), "________")

def q(x,y): if ((x 5)) or ((y 25)): return(“hi") else: return(“low") What do we need to input to get “hi”? What do we need to input to get “low”?

def f(x) if (x > 10): if ((x%2) == 0): return(‘a big, even number’) else: return(‘a big, odd number’) elif (x > 3): if ((x%2) == 0): return(‘a small, even number’) else: return(‘a small, odd number’) else: return(‘a puny number’) assertEqual(f(4),____) assertEqual(f(13),____) assertEqual(f(0),_____)

def ReturnSomething(value): if value = 1: return “glub” else: return “blug” assertEqual(ReturnSomething(1),________)

def nested(x,y): if (y < 0): if (x == "abs"): if (((y * -1) % 2)== 0): return(str(y) + "is even") else: return(str(y) + "is odd") else: if (y % 2 == 0): return(str(y) + "is negative but even") else: return(str(y) + "is negative but odd") else: return("we really don't care if it's even or odd") assertEqual(nested(“abs”,-4),_____________)

def q(x): if (x>5) and (x < 10): return("just enough") elif (x > 5) and (x < 15): return("too much") else: return("no idea") def g(x): if (x > 5): if (x < 10): return("just enough") elif (x < 15): return("too much") else: return("no idea") assertEqual(g(12),________) What about: assertEqual(g(17),________)

def multof3(x): return(x%3) def func(x): if(multof3(x) == 0): return(str(x) + " is a multiple of 3") else: return(str(x) + " is not a multiple of 3") def ismultof3(x): if ((x%3) == 0): return(True) else: return(False) def func2(x): if (ismultof3(x)): return(str(x) + " is a multiple of 3") else: return(str(x) + " is not a multiple of 3")

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)): return(str(x) + " is a multiple of 3") else: return(str(x) + " is not a multiple of 3")

#Determines if input value (x) will solve the #problem: # x 2 - 3x – 4 = 0 #Input: x: a number #Output:a boolean value def eqcheck(x): return x**2 – 3*x – 4 == 0 Given assertEqual(eqcheck(3),______________) What is returned? assertEqual(eqcheck(4),______________)

from cisc106 import * def StateHealth(w,pl,pp,s): return((w *.25) + (pl *.25) + ((100-pp) *.20) + ((100 - s) *.30)) def StateGrade(w,p1,pp,s): if (StateHealth(w,p1,pp,s) > 93): return ("A") elif (StateHealth(w,p1,pp,s) > 84): return('B') elif (StateHealth(w,p1,pp,s) > 75): return('C') elif (StateHealth(w,p1,pp,s) > 66): return('D') else: return('F') assertEqual(StateGrade(80,73,18,19),'C') assertEqual(StateGrade(57,96,55,16),'D') assertEqual(StateGrade(100,100, 0, 0),'A') Remember?

from cisc106 import * def StateHealth(w,pl,pp,s): return((w *.25) + (pl *.25) + ((100-pp) *.20) + ((100 - s) *.30)) def StateGrade(w,p1,pp,s): var1 = StateHealth(w,p1,pp,s) if (var1 > 93): return ("A") elif (var1> 84): return('B') elif (var1> 75): return('C') elif (var1> 66): return('D') else: return('F') assertEqual(StateGrade(80,73,18,19),'C') assertEqual(StateGrade(57,96,55,16),'D') assertEqual(StateGrade(100,100, 0, 0),'A') Instead

from cisc106 import * def StateHealth(w,pl,pp,s): return((w *.25) + (pl *.25) + ((100-pp) *.20) + ((100 - s) *.30)) def StateGrade(w,p1,pp,s): var1 = StateHealth(w,p1,pp,s) + 10 if (var1 > 93): return ("A") elif (var1> 84): return('B') elif (var1> 75): return('C') elif (var1> 66): return('D') else: return('F') assertEqual(StateGrade(80,73,18,19),‘B’) assertEqual(StateGrade(57,96,55,16),‘C') assertEqual(StateGrade(100,100, 0, 0),'A') Add a Curve to the Grade?

from cisc106 import * def calcsomething(par1): if ((par1%2) == 1): return(par1 * -1) else: return(par1) def finddif(par1,par2): var1 = calcsomething(par1) if (var1 < 0): var1 = 0 var2 = calcsomething(par2) if (var2 < 0): var2 = 0 if ((var1 - var2) < 0): return(var2 - var1) else: return(var1 - var2) assertEqual(finddif(3,7),____) assertEqual(finddif(4,7),____) assertEqual(finddif(2,4),____)

def GetMonth(x): if (x == 1): return("January") elif (x == 2): return("February") elif (x == 3): return("March") elif (x == 4): return("April") elif(x == 5): return("May") elif (x == 6): return("June") elif (x == 7): return ("July") elif (x == 8): return("August") elif (x == 9): return("September") elif (x == 10): return("October") elif(x == 11): return("November") elif (x == 12): return("December") def GetDOW(x): if (x == 1): return("Sunday") elif (x == 2): return("Monday") elif (x == 3): return("Tuesday") elif (x == 4): return("Wednesday") elif(x == 5): return("Thursday") elif (x == 6): return("Friday") elif (x == 7): return ("Saturday")

def fu1(x): y = x*3 if (y < 0): y = y * -1 elif (y < 10): y = y + 4 return(y) assertEqual(fu1(-2),__) def fu2(x): y = x*3 if (y < 0): y = y * -1 if (y < 10): y = y + 4 return(y) assertEqual(fu2(-2),__)

def f(x): if (x == 1): return x else: return(x + f(x-1)) assertEqual(f(4),______) def f2(x): if (x == 1): return str(x) else: return(str(x) + f2(x-1)) assertEqual(f2(4),_______)

def f(x): return(x + f(x-1)) assertEqual(f(4),______) def f2(x): if (x == 1): return x else: return(x + f2(x+1)) assertEqual(f2(4),_____) def f3(x): if (x == 1): return x else: return(x + f2(x-2)) assertEqual(f2(4),_______)