Professional Documents
Culture Documents
Lecture 10 Decision Structures PartII
Lecture 10 Decision Structures PartII
languages
Decision Structures- Part II
Lecture 10
Today…
• Last Session:
• Functions- Part II
• Today’s Session:
• Decision Structures- Part II:
• Multi-way Decisions
• A Study on Design
elif Python statement
sandwich_order = “Bacon Roll”
If sandwich_order == “Ham Roll”:
print(“Price:$1.75”)
elif sandwich_order== “cheese roll”:
print(“Price:$1.80”)
eif sandwich_order==“Bacon Roll”:
print(“Price:$2.10”)
else:
print(“Price:$2.00”)
Revisiting Our Quadratic Equation Solver
#The following line will make the math library in Python available for us.
import math
def rootsQEq():
print("This program finds the real solutions to a quadratic.")
print()
print()
print("The solutions are: ", root1, root2)
def rootsQEq():
print("This program finds the real solutions to a quadratic.")
print()
discriminant = b * b – 4 * a * c
A Refined Quadratic Equation Solver
if discriminant < 0:
print(“The equation has no real roots!”)
else:
s_root_val = math.sqrt(discriminant)
root1 = (-b + s_root_val)/(2*a)
root2 = (-b - s_root_val)/(2*a)
rootsQEq()
Revisiting Our Quadratic Equation Solver
• A sample run:
Enter the value of coefficient b: 2
Enter the value of coefficient c: 3
The equation has no real roots!
Good, But…
• This new version of the quadratic solver is certainly a big
improvement, but it still has some quirks!
def rootsQEq():
print("This program finds the real solutions to a quadratic.")
print()
discriminant = b * b – 4 * a * c
Revisiting Our Quadratic Equation Solver
if discriminant < 0:
print("The equation has no real roots!")
elif discriminant == 0:
root = -b/(2*a)
print("\nThere is a double root at", root)
else:
s_root_val = math.sqrt(discriminant)
root1 = (-b + s_root_val)/(2*a)
root2 = (-b - s_root_val)/(2*a)
print("\nThe solutions are: ", root1, root2)
rootsQEq()
Three Sample Runs
This program finds the real solutions to a quadratic.
When discriminant < 0
Please enter the coefficients (a, b, c): 1, 2, 3
The equation has no real roots!
This program finds the real solutions to a quadratic.
When discriminant == 0
Please enter the coefficients (a, b, c): 1, 2, 1
def rootsQEq():
print("This program finds the real solutions to a quadratic.")
print()
discriminant = b * b – 4 * a * c
if a == 0:
print("Cannot divide by zero")
Avoid Dividing By Zero
• Let us now handle the case of avoiding a division by zero
elif discriminant < 0:
print("The equation has no real roots!")
elif discriminant == 0:
root = -b/(2*a)
print("\nThere is a double root at", root)
else:
s_root_val = math.sqrt(discriminant)
root1 = (-b + s_root_val)/(2*a)
root2 = (-b - s_root_val)/(2*a)
print("\nThe solutions are: ", root1, root2)
rootsQEq()
Avoid Dividing By Zero
• Let us try to divide by zero:
This program finds the real solutions to a quadratic.
if x1 >= x2:
if x1 >= x3:
max = x1
else:
max = x3
else:
if x2 >= x3: This program uses a
max = x2
else:
strategy that can be
max = x3 referred to as
“decision tree”
print("The largest value is", max)
main()
Study in Design: Max of Three Numbers (v2)
Yes No
x1 >= x2
Yes No Yes No
X1 >= x3 X2 >= x3
Still, this strategy (i.e., decision tree) makes it very difficult to scale the program to more
numbers (say, 10, 100, or even more)!
Study in Design: Max of Three Numbers (v3)
def main():
x1, x2, x3 = eval(input("Please enter three values: "))
max = x1
if x2 > max:
max = x2
if x3 > max:
max = x3 This program uses a
strategy that can be
print("The largest value is", max) referred to as
“sequential processing”
main()
Study in Design: Max of Three Numbers (v3)
• Interestingly, this version (i.e., v3) of the program allows us to scale to
larger problems
for i in range(10):
print(“Hello”) This will print Hello ten times!
Study in Design: Max of Three Numbers (v3)
def main():
n = eval(input("How many numbers are there? "))
def main():
x1, x2, x3 = eval(input("Please enter three values: "))
• Do not rush to code up the first idea that pops into your head; rather,
think about your design and ask yourself if there is a better way to
solve the problem
• Do not reinvent the wheel– you can use Python’s existing libraries!
Summary
• Decision structures are control structures that allow a program to
execute different sequences of instructions for different cases