Chapter 07
Chapter 07
An Introduction to
Computer Science
Chapter 7
Decision Structures
# convert.py
# A program to convert Celsius temps to Fahrenheit
# by: Susan Computewell
def main():
celsius = float(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is", fahrenheit, "degrees Fahrenheit.")
def main():
celsius = float(input("What is the Celsius temperature? "))
fahrenheit = 9 / 5 * celsius + 32
print("The temperature is", fahrenheit, "degrees fahrenheit.")
if fahrenheit >= 90:
print("It's really hot out there, be careful!")
if fahrenheit <= 30:
print("Brrrrr. Be sure to dress warmly")
main()
import math
def main():
print("This program finds the real solutions to a quadratic\n")
print("\nThe solutionsPython
are:", root1, root2)
Programming, 3/e 29
Two-Way Decisions
As per the comment, when
b2-4ac < 0, the program crashes.
This program finds the real solutions to a quadratic
import math
def main():
print("This program finds the real solutions to a quadratic\n")
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
discrim = b * b - 4 * a * c
if discrim >= 0:
discRoot = math.sqrt(discrim)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print("\nThe solutions are:", root1, root2)
Enter coefficient a: 1
Enter coefficient b: 1
Enter coefficient c: 1
>>>
This is almost worse than the version
that crashes, because we don’t know
what went wrong!
import math
def main():
print "This program finds the real solutions to a quadratic\n"
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
discrim = b * b - 4 * a * c
if discrim < 0:
print("\nThe equation has no real roots!")
else:
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print ("\nThe solutions are:", root1, root2 )
Enter coefficient a: 1
Enter coefficient b: 1
Enter coefficient c: 2
Enter coefficient a: 2
Enter coefficient b: 5
Enter coefficient c: 2
Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 1
def main():
print("This program finds the real solutions to a quadratic\n")
discrim = b * b - 4 * a * c
if discrim < 0:
print("\nThe equation has no real roots!")
elif discrim == 0:
root = -b / (2 * a)
print("\nThere is a double root at", root)
else:
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print("\nThe solutions are:", root1, root2 )
def main():
print ("This program finds the real solutions to a quadratic\n")
try:
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print("\nThe solutions are:", root1, root2)
except ValueError:
print("\nNo real roots")
Enter coefficient a: 1
Enter coefficient b: 1
Enter coefficient c: 1
No real roots
def main():
print("This program finds the real solutions to a quadratic\n")
try:
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print("\nThe solutions are:", root1, root2 )
except ValueError as excObj:
if str(excObj) == "math domain error":
print("No Real Roots")
else:
print("Invalid coefficient given.")
except:
print("\nSomething went wrong, sorry!")
Python Programming, 3/e 59
Exception Handling
The multiple excepts act like elifs. If
an error occurs, Python will try each
except looking for one that matches the
type of error.
The bare except at the bottom acts like
an else and catches any errors without
a specific match.
If there was no bare except at the end
and none of the except clauses match,
the program would still crash and report
an error.
Python Programming, 3/e 60
Exception Handling
Exceptions themselves are a type
of object.
If you follow the error type with an
identifier in an except clause,
Python will assign to that identifier
the actual exception object.
def main():
n = int(input("How many numbers are there? "))