The purpose of this exercise is to understand conditional. Consider the following fragment of code, showing a basic strategy for Blackjack. The goal of the game is the total value of your cards to be higher than that of the dealer's without going over 21 (also known as "busting"). If the total initial value of your cards is 21, it is called blackjack and you automatically win unless the dealer also has a blackjack in which case the hand is a tie.
card1 = ____
card2 = ____
dealer_card = ____
total = card1 + card2
if total == 21:
print 'Blackjack'
elif total >= 17:
print 'Stand'
elif total >= 12:
if dealer_card > 6:
print 'Hit'
else:
print 'Stand'
elif total > 9 or (total == 9 and dealer_card <= 6):
print 'Double then hit'
else:
print 'Hit'
For each of the following card values, write down the output that would be generated. Do this without running the code. It is an important skill to be able to understand what a piece of code does without running it.
For more complete rules, check out this
page. MIT, Blackjack and card counting in the movies (kids
don't try this at home
).
In this exercise, we are going to practice using the if statement. We are going to write a small program that will as the user for the choice player 1 and 2 made and will print out the result of the game. Here are the rules:

First create a truth table for all the possible choices for player 1 and 2 and the outcome of the game, e.g.
| Player 1 | Player 2 | Outcome |
|---|---|---|
Rock |
Rock |
Tie |
Rock |
Scissors |
Player 1 |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
_______ |
This should help you with the next part.
Create a file rsp.py that will generate the outcome of the rock, scissors, paper game. The program should work as follows:
Player 1? rock Player 2? scissors Player 1 wins.
The only valid inputs are rock, paper, and scissors. If the user enteres anything else, your program should output "This is not a valid object selection". Use the truth table you created to help with creating the conditions for your if statement.
*Note* If you have a long condition in your if statement and you want to split it into multiple lines, you would want to enclose the entire expression in parenthesis, e.g.
if (player1 == 'rock' and player2 == 'scissors'): print 'Player 1 wins.'
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps3 rsp.py
For each of the following fragments of code, write what the output would be. Again, do this without running the code.
num = 10
while num > 3:
print num
num = num - 1
divisor = 2
for i in range(0, 10, 2):
print i/divisor
num = 10
while True:
if num < 7:
break
print num
num = num - 1
In this exercise, we will practice using lists and for loops. The goal is to generate the lyrics for the famous American children's song "Old MacDonald Had a Farm". You will start with the program below. Save your program in a file called song.py.
# start with four animals in the farm farm = [ 'horse', 'pig', 'dog', 'cat' ] # display the names of the animals for animal in farm: print animal
Now modify the code to generate this simplified version of the song's lyrics, as follows:
Old MacDonald had a farm, E-I-E-I-O
And on his farm he had a horse, E-I-E-I-O
Here a horse, there a horse, everywhere a horse!Old MacDonald had a farm, E-I-E-I-O
And on his farm he had a pig, E-I-E-I-O
Here a pig, there a pig, everywhere a pig!Old MacDonald had a farm, E-I-E-I-O
And on his farm he had a dog, E-I-E-I-O
Here a dog, there a dog, everywhere a dog!Old MacDonald had a farm, E-I-E-I-O
And on his farm he had a cat, E-I-E-I-O
Here a cat, there a cat, everywhere a cat!
Now modify your code to ask the user to enter the four animals on the farm and then display the song as you did in 4.1.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps3 song.py
This exercise can be done at home. Consider the following program:
n = 10
i = 10
while i > 0:
print i
if i % 2 == 0:
i = i / 2
else:
i = i + 1
Draw a table that shows the value of the variables n and i during the execution of the program. Your table should contain two columns (one for each variable) and one row for each iteration. For each row in the table, write down the values of the variables as they would be at the line containing the print statement.
What is problematic about this program? Suggest one way to improve its behavior.
Enter a number divisible by 2: 11 The number 11 is not divisible by 2. Enter a number divisible by 2: 6 Congratulations! 6 is divisible by 2.
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps3 loops.py
The goal of this exercise is to write a cyclic cipher to encrypt messages. This type of cipher was used by Julius Ceasar to communicate with his generals. It is very simple to generate but it can actually be easily broken and does not provide the security one would hope for.
The key idea behind the Ceasar cipher is to replace each letter by a letter some fixed number of positions down the alphabet. For example, if we want to create a cipher shifting by 3, you will get the following mapping:
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC
To be able to generate the cipher above, we need to understand a little bit about how text is represented inside the computer. Each character has a numerical value and one of the standard encodings is ASCII (American Standard Code for Information Interchange). It is a mapping between the numerical value and the character graphic. For example, the ascii value of 'A' is 65 and the ascii value of 'a' is 97. To conver between the ascii code and the character value in Python, you can use the following code:
letter = 'a' # converts a letter to ascii code ascii_code = ord(letter) # convers ascii code to a letter letter_res = chr(ascii_code) print ascii_code, letter_res
Start small. Do not try to implement the entire program at once. Break the program into parts as follows:
Create a file called cipher.py. Start your program by asking the user for a phrase to encode and the shift value. Then create a new string that contains the original phrase value using a for loop as follows:
encoded_phrase = '' for c in phrase: encoded_phrase = encoded_phrase + c
Now modify the program above to replace all the alphabetic characters with 'x'. For example:
Enter sentence to encrypt: Mayday! Mayday!
Enter shift value: 4
The encoded phrase is: Xxxxxx! Xxxxxx!
We are going to apply the cipher only to the alphabetic characters and we will ignore the others.
Now modify your code, so that it produces the encoded string using the cyclic cipher with the shift value entered by the user. Let's see how one might do a cyclic shift. Let's say we have the sequence:
012345
If we use a shift value of 4 and just shift all the numbers, the result will be:
456789
We want the values of the numbers to remain between 0 and 5. To do this we will use the modulus operator. The expression x%y will return a number in the range 0 to y-1 inclusive, e.g. 4%6 = 4, 6%6 = 0, 7%6 =1. Thus the result of the operation will be:
450123
Hint: Note that the ascii value of 'A' is 65 and 'a' is 97, not 0. So you will have to think how to use the modulus operator to achieve the desired result. Apply the cipher separately to the upper and lower case letters.
Here is what you program should output:
Enter sentence to encrypt: Mayday! Mayday! Enter shift value: 4 The encoded phrase is: Qechec! Qechec!
When you are done, print a copy of the file and turn it in. Make sure your name is in the comment section of your program.
Submit the file electronically by typing:
athena% submit ps3 cipher.py