<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Programming Bootcamp 2016
Lesson 2 Exercises -- ANSWERS
1. Guess the output
Step1:  (^^ you might find it helpful to copy these variables somewhere you can easily see them when doing the problems. e.g. a piece of paper or a text file.)
Step2:  A single = means assignment, and you can't do assignment within a print statement (assignments are always done on their own line). What we want here is
Step3:  What is "in"? This is just another Python operator that you can use in your conditionals. As you may have guessed, (x in y) evaluates to True if x is found in y. This can be used to check if a string is contained in another string (e.g. "Mit" in "Mittens").
2. Guess the output
Step4:  See below for explanation!
Step5:  Surprised by the last two? It's important to note that when you want compare a variable against multiple things, you can only compare it to one thing at a time. Although it makes sense in English to say "is x equal to A or B?", in Python you must write
Step6:  (A) Check if num3 is greater than num4. Print "yes" if it is, and "no" if it isn't.
Step7:  (B) Print a random integer between num2 and num3.
Step8:  (C) Check if the length of the string stored in motif is less than or equal to 25. Print "yes" if it is, and "no" if it isn't.
Step9:  (D) Round num1 to the nearest hundredth.
Step10:  (E) Check if num4 is positive or negative.
Step11:  4. Quadratic formula
Step12:  [ Check your answer ] 
- If a=1, b=4, and c=1, expected output is
Step13:  (B) Adding to your code from part (A)
Step14:  [ Check your answer ] Try running your code above using the following input and make sure your results match the expected output.
Sequence
Step15:  (B) Do this giving the user 3 chances to guess. Only prompt for another guess if the previous guess was incorrect. When they guess correctly, print a confirmation message and end the program. If they don't guess correctly in 3 tries, print "Access denied!" and end the program.
Step16:  7. Coin flip simulation (2pts)
For this problem, we will simulate a coin flip using the random number generator in the random module. 
Try running the following code several times
Step17:  You should see that you always get either 0 or 1. We will pretend that each time we run random.randint(0,1) we are flipping a coin, where 1 represents heads and 0 represents tails.
Write code that "flips a coin" 10 times and counts how many times it comes up heads. At the end, print how many of the flips were heads.
Step18:  Extra problems (0pt)
The following problems are for people who would like more practice. They will not be counted for points.
(A) Following from the coin flip problem above
Step19:  (B) Using raw_input(), prompt the user for a number. Check if that number is between 50 and 100, and print different messages depending on the outcome.
Step20:  (C) Using raw_input(), prompt the user for an integer. Check if the integer is a multiple of 7, and print different messages depending on the outcome.
<ASSISTANT_TASK:>
Python Code:
# RUN THIS BLOCK FIRST TO SET UP VARIABLES!
a = True
b = False
x = 2
y = -2
cat = "Mittens"
Explanation: Programming Bootcamp 2016
Lesson 2 Exercises -- ANSWERS
1. Guess the output: conditionals practice (1pt)
For the following blocks of code, first try to guess what the output will be, and then run the code yourself. Points will be given for filling in the guesses; guessing wrong won't be penalized.
NOTE: The first cell below holds the variables that will be used in the problems. Since variables are shared across cells in Jupyter notebooks, you just need to run this cell once and then those variables can be used in any other code cell. 
(However, remember that closing the notebook or restarting the kernel clears all the variables, so you will need to re-run the cell to re-populate those variables after doing that.)
End of explanation
print a
print (not a)
print (a == b)
print (a != b)
print (x == y)
print (x > y)
print (x = 2)
Explanation: (^^ you might find it helpful to copy these variables somewhere you can easily see them when doing the problems. e.g. a piece of paper or a text file.)
End of explanation
print (a and b)
print (a and not b)
print (a or b)
print (not b or a)
print not (b or a)
print (not b and a)
print not (b and a)
print (x == abs(y))
print len(cat)
print cat + x
print cat + str(x)
print float(x)
print ("i" in cat)
print ("g" in cat)
print ("Mit" in cat)
Explanation: A single = means assignment, and you can't do assignment within a print statement (assignments are always done on their own line). What we want here is: print (x == 2)
End of explanation
# RUN THIS BLOCK FIRST TO SET UP VARIABLES!
x = 2
y = -2
cat = "Mittens"
if (x % 2) == 0:
    print "x is even"
else:
    print "x is odd"
if (y - 4*x) < 0:
    print "Invalid!"
else:
    print "Banana"
if "Mit" in cat:
    print "Hey Mits!"
else:
    print "Where's Mits?"
x = "C"
if x == "A" or "B":
    print "yes"
else:
    print "no"
Explanation: What is "in"? This is just another Python operator that you can use in your conditionals. As you may have guessed, (x in y) evaluates to True if x is found in y. This can be used to check if a string is contained in another string (e.g. "Mit" in "Mittens").
2. Guess the output: if statement practice (1pt)
Same directions as above.
End of explanation
x = "C"
if (x == "A") or (x == "B"):
    print "yes"
else:
    print "no"
Explanation: See below for explanation!
End of explanation
# RUN THIS BLOCK FIRST!
num1 = 3.14159
num2 = 6
num3 = 100
num4 = 6 * -5 - 4 * 2 + -7 * -8 + 3
motif = "GTAAGTCGGTAACGTAAGTCGGTAAC"
Explanation: Surprised by the last two? It's important to note that when you want compare a variable against multiple things, you can only compare it to one thing at a time. Although it makes sense in English to say "is x equal to A or B?", in Python you must write: ((x == "A") or (x == "B")) to accomplish this. The same goes for e.g. ((x > 5) and (x < 10)) and anything along those lines.
So why does the first version give the answer "yes"? Basically, anything that isn't False or the literal number 0 is considered to be True in Python. When you say 'x == "A" or "B"', Python reads it as '(x == "A") or (B)', which evaluates to '(False) or (True)', which is True! 
3. On your own (2pts)
Write code to accomplish the following tasks using the concepts we went over in the lecture. 
Use the following variables in your code. You must actually use the variables; pretend you don't know their values.
End of explanation
if num3 > num4:
    print "Yes"
else:
    print "No"
Explanation: (A) Check if num3 is greater than num4. Print "yes" if it is, and "no" if it isn't.
End of explanation
import random
random.randint(num2, num3)
Explanation: (B) Print a random integer between num2 and num3.
End of explanation
if len(motif) <= 25:
    print "Yes"
else:
    print "No"
Explanation: (C) Check if the length of the string stored in motif is less than or equal to 25. Print "yes" if it is, and "no" if it isn't.
End of explanation
round(num1, 2)
Explanation: (D) Round num1 to the nearest hundredth.
End of explanation
if num4 >= 0:
    print "It's positive"
else:
    print "It's negative"
Explanation: (E) Check if num4 is positive or negative.
End of explanation
a = float(raw_input("Enter value for a: "))
b = float(raw_input("Enter value for b: "))
c = float(raw_input("Enter value for c: "))
underRoot = (b**2 - 4*a*c)
if underRoot >= 0:
    x1 = ( (-b) + underRoot ** 0.5 ) / float(2*a)
    x2 = ( (-b) - underRoot ** 0.5 ) / float(2*a)
    print "x =", x1, "or", x2
else:
    print "non-real answer"
Explanation: 4. Quadratic formula: checking for negative roots (1pt)
Recall that when calculating the quadratic formula, you will get an error if $b^2 - 4ac$ is negative, since you can't take the square root of a negative number. 
Edit the code below so that it checks for this potential error before it occurs: 
- If the error is going to occur, print a message saying "non-real answer" and do not calculate the values of x. 
- If the error is not going to occur, calculate and print the values of x.
End of explanation
dnaSeq = raw_input("Enter a DNA sequence: ")
motif = raw_input("Enter a motif to search for: ")
if len(motif) > len(dnaSeq):
    print "Motif is too long."
Explanation: [ Check your answer ] 
- If a=1, b=4, and c=1, expected output is:
x = -0.267949192431 or -3.73205080757
- If a=2, b=2, and c=2, expected output is:
non-real answer
5. Motif checker (2pts)
Using raw_input(), prompt the user for a DNA sequence and a motif to search for (the sequence and motif can be any string of A/T/G/C's; see below for examples).
(A) Find the length of the motif and length of the DNA sequence and check if the motif is longer than the sequence. If it is, print "Motif is too long".
End of explanation
dnaSeq = raw_input("Enter a DNA sequence: ")
motif = raw_input("Enter a motif to search for: ")
if len(motif) > len(dnaSeq):
    print "Motif is too long."
else:
    if motif in dnaSeq:
        print "Motif found."
    else:
        print "Motif not found."
Explanation: (B) Adding to your code from part (A): If and only if the motif is shorter than the sequence, go on to check if the motif can be found somewhere within the sequence (hint: use "in"). If it is found, print "Motif found". If it is not found, print "Motif not found".
End of explanation
password = "Mittens123"
guess = raw_input("Enter the password: ")
if guess == password:
    print "Correct!"
else:
    print "Incorrect password. Access denied."
Explanation: [ Check your answer ] Try running your code above using the following input and make sure your results match the expected output.
Sequence: AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG <br>
Motif: ATG <br>
Expected output:
Motif not found
Sequence: AGCTAGCCTGCTAGAAATCGATTGGCTAGCAATCTTATTGTGTTCTACG <br>
Motif: ATCGA <br>
Expected output:
Motif found
Sequence: CTAGCC <br>
Motif: ATGGCTAGCTA <br>
Expected output:
Motif is too long
6. Password protection (3pts)
Write code that prompts the user to guess a password and checks whether the password is correct. 
First create a variable called password and set it equal to whatever you want the password to be (any string). 
Then prompt the user to guess the password, and read their input from the terminal. 
Check if what they entered matches your password, and print a message saying whether or not the guess was correct.
(A) Do this giving the user only one chance to guess.
End of explanation
password = "Mittens123"
guess = raw_input("Enter the password: ")
if guess == password:
    print "Correct!"
else:
    guess = raw_input("Incorrect password. Try again: ")
    if guess == password:
        print "Correct!"
    else:
        guess = raw_input("Incorrect password. Try again: ")
        if guess == password:
            print "Correct!"
        else:
            print "Incorrect password. Access denied."
Explanation: (B) Do this giving the user 3 chances to guess. Only prompt for another guess if the previous guess was incorrect. When they guess correctly, print a confirmation message and end the program. If they don't guess correctly in 3 tries, print "Access denied!" and end the program.
End of explanation
import random
randNum = random.randint(0,1)
print randNum
Explanation: 7. Coin flip simulation (2pts)
For this problem, we will simulate a coin flip using the random number generator in the random module. 
Try running the following code several times:
End of explanation
import random
headsCount = 0
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
if random.randint(0,1) == 1:
    headsCount = headsCount + 1
print headsCount, "of 10 flips were heads"
Explanation: You should see that you always get either 0 or 1. We will pretend that each time we run random.randint(0,1) we are flipping a coin, where 1 represents heads and 0 represents tails.
Write code that "flips a coin" 10 times and counts how many times it comes up heads. At the end, print how many of the flips were heads.
End of explanation
import random
headsCount = 0
# random.randint(0,3) gives a 0, 1, 2, or 3
# if it's 0, 1, or 2, call it heads
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
if random.randint(0,3) < 3:
    headsCount = headsCount + 1
print headsCount, "of 10 flips were heads"
Explanation: Extra problems (0pt)
The following problems are for people who would like more practice. They will not be counted for points.
(A) Following from the coin flip problem above:
Change your code so that the coin is now "unfair" (i.e. the chance of heads is not equal to the chance of tails). See if you can make it so the probability of heads is 75%. 
Hint: There are several ways to do this, but one simple way involves using a larger range of random ints and assigning heads/tails differently.
End of explanation
num = int(raw_input("Enter a number: "))
if (num >= 50) and (num <= 100):
    print "Your number is between 50 and 100"
else:
    print "Your number is not between 50 and 100"
Explanation: (B) Using raw_input(), prompt the user for a number. Check if that number is between 50 and 100, and print different messages depending on the outcome.
End of explanation
num = int(raw_input("Enter a number: "))
if (num % 7) == 0:
    print "Your number is a multiple of 7"
else:
    print "Your number is not a multiple of 7"
Explanation: (C) Using raw_input(), prompt the user for an integer. Check if the integer is a multiple of 7, and print different messages depending on the outcome.
End of explanation
<END_TASK>