Python Programming: An Introduction To Computer Science: Decision Structures
Python Programming: An Introduction To Computer Science: Decision Structures
Python Programming: An Introduction To Computer Science: Decision Structures
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 = eval(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is", fahrenheit, "degrees Fahrenheit.")
main()
def main():
celsius = eval(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")
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
import math
def main():
print("This program finds the real solutions to a quadratic\n")
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)
import math
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!")
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 )
main()
Python Programming, 2/e 38
Two-Way Decisions
>>>
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, b, c = eval(input("Please enter the coefficients (a, b, c): "))
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 )
import math
def main():
print("This program finds the real solutions to a quadratic\n")
try:
a, b, c = eval(input("Please enter the coefficients (a, b, 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")
No real roots
def main():
print("This program finds the real solutions to a quadratic\n")
try:
a, b, c = eval(input("Please enter the coefficients (a, b, 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("You didn't give me the right number of coefficients.")
except NameError:
print("\nYou didn't enter three numbers.")
except TypeError:
print("\nYour inputs were not all numbers.")
except SyntaxError:
print("\nYour input was not in the correct form. Missing comma?")
except:
print("\nSomething went wrong, sorry!")
def main():
n = eval(input("How many numbers are there? "))