# The price is right def printItems( items ): print "Please choose the number of one of the following items" index = 0 for i in items: print index, ":", i index += 1 def playTry1(): print "Welcome to the price is right!" print "Your goal is to buy 5 or fewer items (any number of each item)" print "and spend between $9.25 and $10" print items = ["bleach", "coke", "ramen", "ice cream", "super ball"] prices = [1.35, .75, .25, 3.00, .50] money = 0.0 while money < 9.25 and len(items) > 0: print print "You have spent $", money printItems( items ) choice = input( "Which item would you like to buy? ") #while not(validChoice(choice, chosen, len(items))): # print choice, "is not a valid choice" # choice = input( "Which item would you like to buy? ") number = input( "How many " + items[choice] + " would you like?") print items[choice], "is $", prices[choice] money += prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:] print "You have spent $", money if money >= 9.25 and money <= 10.0: print "You win!" else: print "You lose!" def playBetter(): """ Play the game """ # Initialize all of the constants and variables [items, prices] = initilizeItems() LIMIT_HIGH = 10.00 LIMIT_LOW = 9.50 money = 0.0 printIntro(LIMIT_LOW, LIMIT_HIGH, items) while not allDone(items, LIMIT_LOW, money): print "You have spent $", money [items, prices, spent] = playRound(items, prices) money += spent # Game is over, print a message winOrLose(money, LIMIT_LOW, LIMIT_HIGH) def initilizeItems(): """ Initialize the list of items and their prices return a list [items, prices] """ items = ["bleach", "coke", "ramen", "ice cream", "super ball"] prices = [1.35, .75, .25, 3.00, 1.00] return [items, prices] def printIntro(minspend, maxspend, items): print "Welcome to the price is right!" print "Your goal is to buy", len(items), "or fewer items (any number of each item)" print "and spend between", minspend, "and", maxspend print def playRound(items, prices): """ Play one round of the game Inputs: A list of items and a list of prices Returns: [items, prices, spent] where items is list of items remaining, prices is a list of remaining prices, and spent is the amount spent this round""" print "*********************" printItems( items ) choice = input( "Which item would you like to buy? ") number = input( "How many " + items[choice] + " would you like? ") print items[choice], "is $", prices[choice] spent = prices[choice]*number prices = prices[0:choice] + prices[choice+1:] items = items[0:choice] + items[choice+1:] return [items, prices, spent] def allDone( items, limitLow, money ): """ Returns whether or not the game is over. Inputs: items, the list of remaining items limitLow, the minimum amount to spend money, the money spent """ return money >= limitLow or len(items) == 0 def winOrLose(money, minspend, maxspend): """ Determine whether the player won or lost and print a message """ print "You spent $", money if money >= minspend and money <= maxspend: print "You win!" else: print "You lose." def main(): playBetter() if __name__ == "__main__": main()