Lecture 13 + (LAB04)

posted Feb 21, 2013, 8:44 AM by Samuel Konstantinovich   [ updated Feb 21, 2013, 8:44 AM ]

Goals: converting to string, more practice with functions, formatting output.
You need to:

import math

Then you can:


Write a function quadsolve that takes the coefficients of a quadratic equation a b and c, as well as a variable pos. When pos is +1 return the positive root of the equation, when pos is -1, return the negative root of the equation.
Examples of quadsolve(a,b,c,pos):
quadsolve(1,1,-6,1) →2

Lets format our output better!

showQuadSolve(A,B,C) → prints nicely formatted results


>>> showQuadSolve(1,2,1)

Positive root of 1x^2 + 2x + 1: -1.0

Negative root of 1x^2 + 2x + 1: -1.0

>>> showQuadSolve(1,-5,-50)

Positive root of 1x^2 + -5x + -50: 10.0

Negative root of 1x^2 + -5x + -50: -5.0

We can't just add the numbers to the string

print "the answer is " + 5 #this doesn't work (try it)

To do this you need to be able to combine variables with your string:

str(x) converts x to a string. This can be used when you want to combine numerical values with strings of text:


"I am" + str(age) + "years old"


"The root of " + str(A) + "x^2 . . . .. more text"