Contents
- dealornodeal.py
- game.py
- majors_dictionary3.py
- majors_dictionary4.py
dealornodeal.py 1/4
[top][prev][next]
# Simulates Deal or No Deal
# by Sara Sprenkle, 10.17.2007
# Modified by ....
from random import shuffle
# number of choices in each round
CHOICES=[6,5,4,3,2,1,1,1,1]
TOTALROUNDS=len(CHOICES)
#indicates that the case has been chosen and that the
#amount is off the board
CHOSEN=-1
def main():
heading= "*************************************"
print heading
print "Welcome to Deal or No Deal!".center(len(heading))
print heading
print
# Set up the cases with their dollar amounts.
# caseValues will be used to display the case values that haven't
# been revealed
caseValues = readCaseValues()
# copy the case values in another list to keep track of which
# cases have been opened.
cases = caseValues + []
# shuffle the cases to assign values to cases, randomly
shuffle(cases)
choice = getUserChoice(cases,"Which case do you want to open? (0-25) ")
print "Interesting choice!"
# keep track of how much was in your case and mark the case as
# chosen.
amtInCase = cases[choice]
cases[choice] = CHOSEN
printBoard(caseValues)
noDeal = True
round = 0
offers = []
# play until the user hasn't made a deal and we haven't exhausted
# cases to open
while noDeal and round < len(CHOICES):
numChoices = CHOICES[round]
printCasesLeft(cases)
print "You can open", numChoices, "cases"
# Let the user open cases and reveal the cases' values
for x in xrange(numChoices):
prompt = "Which case do you want to open? (You have "
prompt += str(numChoices-x) + " left) "
chosenCase = getUserChoice(cases,prompt)
caseValue = cases[chosenCase]
# update the selected case and the case values
cases[chosenCase] = CHOSEN
index = caseValues.index(caseValue)
caseValues[index] = CHOSEN
print "You chose case", chosenCase
print "It contains ... $%.2f" % caseValue
# The board contains ...
printBoard(caseValues)
offer = makeOffer(caseValues, round)
print "\nYour current offer is $%d." % offer
if len(offers) > 0 :
print "Your previous offers were"
for prevOff in offers:
print "\t$%d" % prevOff
answer = raw_input("Deal or No Deal? ")
answer = answer.lower()
print
# if the user makes a deal ...
if answer.startswith("deal") or answer == "yes" :
noDeal = False
print "You have won $%d" % offer
print "\nYour case contained $%.2f." % amtInCase
if amtInCase > offer:
print "Well, you still won money--more than you had when you came!"
else:
print "You made a great deal!"
round += 1
offers.append(offer)
# opened all the cases
print
print "You have won your case, which contained $%.2f!" % amtInCase
noDeal = False
# read the values of the cases from a file
# output: return the list of case values
def readCaseValues():
valFile = file("case_values.txt", "r")
values = []
for line in valFile:
# values in cases need to be floats, not strings
val = float(line)
values.append(val)
valFile.close()
return values
# print the cases that haven't been chosen
# input: the list of cases
def printCasesLeft(cases):
print "Cases Left to Choose from:"
count = 0
linecount = 1
for caseAvail in cases:
# only print if the case hasn't been chosen yet
if caseAvail != CHOSEN :
print "%3d" % count,
# print a newline after every fourth printed case
if linecount % 4 == 0:
print
linecount += 1
count += 1
print
# print the amounts left on the board (the value of cases that haven't
# been revealed yet)
# input: the list of case values
def printBoard(amounts):
border = "*****"*6
print
print border
print " The Board: "
count = 0
for count in xrange(len(amounts)/2):
if amounts[count] != CHOSEN:
print "$%10.2f " % amounts[count],
else:
print "%11s " % "----",
second_col = len(amounts)/2 + count
if amounts[second_col] != CHOSEN:
print "$%10.2f " % amounts[second_col]
else:
print "%11s " % "----"
print border
# input: the case values (if chosen or not) and which round we're in
# output: return the banker's offer
def makeOffer(amounts, round):
numCases = 0
totalAvail = 0
if round < TOTALROUNDS/3:
weight = 1.7
elif round < TOTALROUNDS/4*3:
weight = 1.4
else:
weight = 1.2
#print "Weight is ", weight
for val in amounts:
if val != CHOSEN:
totalAvail += val
numCases +=1
offer= totalAvail/(numCases*weight)
return int(offer)
# input: the list of cases and the choice
# output: return True iff the choice is a valid choice (has not
# already been chosen and is a valid case number)
def isValidChoice(cases, choice):
if choice < 0 or choice > len(cases):
return False
if cases[choice] == CHOSEN:
return False
return True
# repeatedly prompts the user for a case number until the user selects
# a valid case
# input: the list of cases and the prompt
# output: returns the user's [valid] choice
def getUserChoice(cases, prompt):
choice = input(prompt)
while not isValidChoice(cases, choice):
print "Not a valid choice"
choice = input(prompt)
return choice
main()
game.py 2/4
[top][prev][next]
# Game Module
# Contains useful functions and modules for various games
# by Sara Sprenkle, 10.24.2007
import random
# constants for flipping a coin
HEADS = 0
TAILS = 1
def testMe():
rollDie(6)
rollDie()
rollDie(12)
# No pre- or post- condition
# return HEADS or TAILS, randomly
def coinFlip():
return random.randint(0,1)
# input: the number of sides of the die (defaults to 6)
# return a random number between 1 and sides, inclusive
def rollDie(sides=6):
#print "Num sides =", sides
return random.randint(1,sides)
# Remove this line after done testing.
# Otherwise, will call the testMe function when game module is
# imported.
testMe()
majors_dictionary3.py 3/4
[top][prev][next]
# Read in majors from a file and put into a dictionary.
# by Sara Sprenkle, 10.24.2007
UNDECLARED="Undeclared"
def main():
MAJORSFILE="data/majors.all.dat"
majorsData = file(MAJORSFILE, "r")
# create an empty dictionary that will map last names to majors
majorToCount={}
for line in majorsData:
line = line.strip()
# FIRST " " SPLITS NAME AND MAJORS
spacePos = line.find(" ")
if spacePos== -1:
# person is undeclared
addStudent( majorToCount, name)
else:
name = line[:spacePos]
# print "name=", name
major = line[spacePos+1:]
# print "major=", major
addStudent( majorToCount, name, major)
majorsData.close()
printMajorInfo(majorToCount)
# Update the mapping of majors to number of majors.
# Input: dictionary of majors to number of majors (majorToCount),
# string name, optional string major (defaults to Undeclared)
def addStudent(majorToCount, name, major=UNDECLARED):
if majorToCount.get(major) != None:
majorToCount[major]+=1
else:
majorToCount[major]=1
# Print the number of majors for each major, sorting
# alphabetically by the major
# Input: mapping of majors to the number of majors
def printMajorInfo(majorToCount):
# not actually sorted at this point
sortedKeys = majorToCount.keys()
sortedKeys.sort()
print "%25s %5s" %( "MAJOR", "NUM")
print "-"*31
for major in sortedKeys:
count = majorToCount[major]
print "%25s %5d" % (major, count)
main()
majors_dictionary4.py 4/4
[top][prev][next]
# Read in majors from a file and put into a dictionary.
# by Sara Sprenkle, 10.24.2007
UNDECLARED="Undeclared"
def main():
MAJORSFILE="data/majors.all.dat"
majorsData = file(MAJORSFILE, "r")
# create an empty dictionary that will map last names to majors
majorToCount={}
for line in majorsData:
line = line.strip()
# FIRST " " SPLITS NAME AND MAJORS
spacePos = line.find(" ")
if spacePos== -1:
# person is undeclared
addStudent( majorToCount, name)
else:
name = line[:spacePos]
# print "name=", name
major = line[spacePos+1:]
# print "major=", major
addStudent( majorToCount, name, major)
majorsData.close()
printMajorInfo(majorToCount)
# Update the mapping of majors to number of majors.
# Input: dictionary of majors to number of majors (majorToCount),
# string name, optional string of majors (defaults to Undeclared)
def addStudent(majorToCount, name, major=UNDECLARED):
majors = major.split()
for major in majors:
if majorToCount.get(major) != None:
majorToCount[major]+=1
else:
majorToCount[major]=1
# Print the number of majors for each major, sorting
# alphabetically by the major
# Input: mapping of majors to the number of majors
def printMajorInfo(majorToCount):
# not actually sorted at this point
sortedKeys = majorToCount.keys()
sortedKeys.sort()
print "%25s %5s" %( "MAJOR", "NUM")
print "-"*31
for major in sortedKeys:
count = majorToCount[major]
print "%25s %5d" % (major, count)
main()
Generated by GNU enscript 1.6.4.