Python Homework Help
Python Homework Help
In this problem, we'll create a login screen, where the user must
enter a password in order to see a secret message. We will give the
user 3 chances to get the password right, and either print the
secret message or a failure message (after 3 chances).
First, define a function encrypt that takes one string. It will hash
the string using the built-in Python function hash (try it on the
shell) and modulo the value by a prime number (e.g. 541 -- this is
very small in the computer science world but good enough for us).
The function should then return this number.
e.g. encrypt("my password") -> 283 (if you use 541 as the prime, for
example)
At the top of the file, define a variable _KEY to be the result, e.g.
_KEY = 283.
Now, write the rest of the program. Each time you ask the user for
the password, call encrypt with the user's input, and compare the
value to _KEY. If the two match, the user (most likely) entered the
correct password, otherwise he loses one chance.
Problem 2 –
The game of Nims / Stones
In this game, two players sit in front of a pile of 100 stones. They
take turns, each removing between 1 and 5 stones (assuming there
are at least 5 stones left in the pile). The person who removes the
last stone(s) wins.
Write a program to play this game. This may seem tricky, so break it
down into parts. Like many programs, we have to use nested loops
(one loop inside another).
So, the basic outline of the program should be something like this:
TOTAL = 100
MAX = 5
pile = TOTAL # all stones are in the pile to start
while [pile is not empty]:
while [player 1's answer is not valid]:
[ask player 1]
[check player 1's input... is it valid?]
[same as above for player 2]
Note how the important numbers 100 and 5 are stored in a single
variable at the top. This is good practice -- it allows you to easily
change the constants of a program. For example, for testing, you
may want to start with only 15 or 20 stones.
Solution
# Some constants...
LARGE_PRIME = 541
_KEY = 171 # to get this number, I used the password "solution"
MAX_FAILURES = 3 # stop when we hit this many failures
prompt = "Player " + str(player) + ", there are " + str(pile_size) + "
stones in front of you. " + \
"How many stones would you like to remove (1-5)?\n"
move = int(raw_input(prompt))
if move <= 0:
print "Hey! You have to remove at least one stone!"
#If we've exited the loop, the pile size is 0. The player whose turn it
is NOW just lost the game..
print "Player", 2-(player-1), "has won the game!"
# nims.py
# Example solution for Lab 4, problem 2.
#
# Aseem Kishore
#
# 6.189 - Intro to Python
# IAP 2008 - Class 3
TOTAL = 100
MAX = 5
pile = TOTAL # number of stones in the pile at any given time
def is_valid(x):
# returns True iff x is between 1 and MAX, and there's also
enough stones,
# otherwise returns False
return (x >= 1) and (x <= MAX) and (x <= pile)
while pile > 0:
# player 1 turn
print "Player 1's turn. There are", pile, "stones in the pile."
x=0
while not is_valid(x):
# ask
x = int(raw_input("Player 1, how many? "))
# check
if not is_valid(x):
print "That's not valid, it has to be between 1 and 5, and",\
"you can't pick up more than there are in the pile."
pile = pile - x
if pile == 0:
# win -- do something
print "Congratulations, Player 1, you win!"
break
# player 2 turn
print "Player 2's turn. There are", pile, "stones in the pile."
y=0
while not is_valid(y):
y = int(raw_input("Player 2, how many? "))
if not is_valid(x):
print "That's not valid, it has to be between 1 and 5, and",\
"you can't pick up more than there are in the pile."
pile = pile - y
if pile == 0:
# win -- do something
print "Congratulations, Player 2, you win!"
break
print "Game over."