import random # Author: James Tam # Version: 2022 MIN_NUM = 1 MAX_NUM = 10 MIN_COLOR = 1 MAX_COLOR = 3 MAX_GUESSES = 10 RED = 1 BLUE = 2 GREEN = 3 gameWon = False while (gameWon == False): #Main loop for program, repeat playing game until won stillGuessingNum = True guessedNumber = False i = 1 while (stillGuessingNum == True): #Loop for game 1: guessing a number actualNum = random.randrange(MIN_NUM,MAX_NUM)+1 print("Guess a # from %d-%d: " %(MIN_NUM,MAX_NUM), end="") playerNum = int(input()) if (actualNum == playerNum): print("Guessed %d!" %(actualNum)) stillGuessingNum = False guessedNumber = True i = i + 1 if (i > MAX_GUESSES): stillGuessingNum = False guessedNumber = True print("Exceeded maximum tries of %d" %(MAX_GUESSES)) stillGuessingColor = True guessedColor = False i = 1 while (stillGuessingColor == True): #Loop for game 2: guessing the color numericColor = random.randrange(MIN_COLOR,MAX_COLOR)+1 actualColor = "" if (numericColor == RED): actualColor = "red" if (numericColor == BLUE): actualColor = "blue" if (numericColor == GREEN): actualColor = "green" playerColor = input("Guess color (red, blue, green): ") if (playerColor == actualColor): print("Guessed actual the color %s" %(actualColor)) stillGuessingColor = False guessedColor = True i = i + 1 if (i > MAX_GUESSES): stillGuessingColor = False print("Exceeded maximum tries of %d" %(MAX_GUESSES)) #Check if both games won. NOTE: this branch changes a flag to break out #of the main program loop. A break is NOT used (spegetti programming is #regarded as bad style. if ((guessedNumber == True) and (guessedColor == True)): gameWon = True print("You won the game!")