Instructions: For each problem, write code in the provided code block. Don't forget to run your code to make sure it works.
1. Write code to print the following string:
Insert some funny string here
print "I love Python"
2. The following code blocks have errors. Fix each one so that it runs without error.
print "I bought" , 10 , "pizzas"
firstPerson = "Joe"
print firstPerson
fruit = "Banana"
print "I bought a", fruit
3. The equation for a line is
y = mx + b
Write code to solve for y for a given m, x, and b. Use the code below as a starting point, and fill in the rest of the needed code.
m =
x =
b =
y=m*x+b
print y
Write code to calculate the quadratic formula: Your code should output both possible values for x. The program should start by defining the values of a, b, and c. For example, the first three lines of your script might look like this:
a = -2
b = 2
c = 1
If you use these values, the answers should be -0.366 and 1.366 (check and make sure you get this). Try a few different values of these variables. Note, you will get an error message if
b2 - 4ac
is negative, since you can't take the square root of a negative number. This is fine for now -- later we'll go over ways to prevent bugs like that from occurring.
Hint: There are a few different ways to find the square root in Python. Try googling it.
a = -2
b = 2
c = 1
delta = (b**2-4*a*c)**0.5
print "x=",(delta-b)/(2*a),"or",(-delta-b)/(2*a)