import random Cards = [0,0,0] def AskQuestion(prompt): gotAns = False retVal = "" while(not gotAns): ans = input("{0}? ".format(prompt)) if(ans == ""): print("PLEASE ANSWER THE QUESTION.") else: gotAns = True retVal = ans.upper() return retVal def IsAnInt(input): retVal = True try: i = int(input) except: retVal = False return retVal def GetIntInput(prompt): gotAns = False retVal = 0 while(not gotAns): ans = input("{0}: ".format(prompt)) if(IsAnInt(ans) == True): gotAns = True retVal = int(ans) else: print("YOU DID NOT ENTER AN INTEGER.") return retVal def AskForBet(wallet): gotBet = False bet = 0 while(not gotBet): bet = GetIntInput("WHAT IS YOUR BET?") if(bet < 0): print("PLEASE BET A NON-NEGATIVE AMOUNT.") elif(bet > wallet): print("SORRY MY FRIEND, BUT YOU BET TOO MUCH.") print("YOU HAVE ONLY {0} DOLLARS TO BET.".format(wallet)) else: gotBet = True return bet def AskToPlayAgain(): gotAns = False retVal = False while(not gotAns): ans = AskQuestion("DO YOU WANT TO PLAY AGAIN") if(ans == "YES" or ans == "Y"): retVal = True gotAns = True elif(ans == "NO" or ans == "N"): retVal = False gotAns = True else: print("PLEASE ANSWER \"YES\" OR \"NO\".") return retVal def AskToSeeTheRules(): gotAns = False retVal = False while(not gotAns): ans = AskQuestion("DO YOU WANT TO SEE THE RULES") if(ans == "YES" or ans == "Y"): retVal = True gotAns = True elif(ans == "NO" or ans == "N"): retVal = False gotAns = True else: print("PLEASE ANSWER \"YES\" OR \"NO\".") return retVal def DisplayCard(card): if(card == 11): print("JACK") elif(card == 12): print("QUEEN") elif(card == 13): print("KING") elif(card == 14): print("ACE") else: print("{0}".format(card)) def DisplayRules(): print("ACEY DUCEY IS PLAYED IN THE FOLLOWING MANNER:") print("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP") print("YOU HAVE THE OPTION TO BET OR NOT BET DEPENDING") print("ON WHETHER OR NOT YOU FEEL THE NEXT CARD WILL ") print("HAVE A VALUE BETWEEN THE FIRST TWO.") print(" ") print("IF YOU DO NOT WANT TO BET, INPUT A 0 (ZERO).") def GenerateRandomNumber(minimum, maximum, show): retVal = random.randint(minimum, maximum) if(show): print("{0}".format(retVal)) return retVal def SelectCard(): gotCard = False while(not gotCard): card = GenerateRandomNumber(2,14,False) if(Cards[0] != card and Cards[1] != card and Cards[2] != card): gotCard = True DisplayCard(card) return card def ClearCards(): Cards[0] = 0 Cards[1] = 0 Cards[2] = 0 def Run(): print("*** ACEY DUCEY : PYTHON ***") random.seed() Running = True if(AskToSeeTheRules() == True): DisplayRules() while(Running): Wallet = 100 GameOver = False while(not GameOver): ClearCards() print("YOU NOW HAVE {0} DOLLARS.".format(Wallet)) Cards[0] = SelectCard() Cards[1] = SelectCard() Bet = AskForBet(Wallet) if(Bet > 0): Cards[2] = SelectCard() if((Cards[2] > Cards[0] and Cards[2] < Cards[1]) or (Cards[2] > Cards[1] and Cards[2] < Cards[0])): print("YOU WIN!!!") Wallet = Wallet + Bet else: print("SORRY, YOU LOSE.") Wallet = Wallet - Bet if(Wallet < 1): print("SORRY FRIEND, YOU ARE BROKE.") GameOver = True else: GameOver = True print("*** GAME OVER ***") Running = AskToPlayAgain() print("OK, HOPE YOU HAD FUN.") print("THANKS FOR PLAYING!") Run()