Python fun guessing game.

I recently started exploring Django and Flask to determine if I could migrate my application to these frameworks. However, as I hadn’t coded in Python in a while, I decided to create a small program to refresh my skills. I chose to create a fun guessing game, which is a popular coding exercise taught in schools. I asked my daughter to play the game and she gave it her seal of approval, saying that she had fun playing it. In fact, she was so enthusiastic about it that she now wants to turn the game into an iPad app, incorporating her own ideas to make it even better. Unfortunately, I am not at that level yet. Baby steps for daddy, my child.

Below is the code.




import random

cpu = 0
player = 0
play = 'y'
option = 0

print("\n Welcome to Guess My Number")
print("********** DIFFICULTY **********")
print("1 - Chicken - (5 Guesses)")
print("2 - Turkey - (10 Guesses)")
print("3 - Beef - (20 Guesses)")
print("********** ********** **********")
while True:
try:
option = int(input("Enter difficulty: "))
if option == 1:
gameLevel = 5
break
elif option == 2:
gameLevel = 10
break
elif option == 3:
gameLevel = 20
break
else:
print("That is not a valid option!")
except:
print("That is not a valid option!")

while play == 'y':

winner = 0
numberToGuess = random.randrange(1,99)

while winner == 0:
while True:
try:
playerGuess = int(input("\nEnter your Guess: "))
break
except:
print("That is not a valid option!")
if playerGuess == numberToGuess:
print("\nYour guess is", playerGuess,"\033[33mWinner Winner Chicken Dinner!\033[0m")
player = player + 1
winner = 1
elif playerGuess > numberToGuess:
print("Your input is ", playerGuess,"Too High\n")
elif playerGuess < numberToGuess: print("Your input is ", playerGuess,"Too Low\n") 
if winner != 1: for x in range(gameLevel): cpuGuess = random.randrange(1,99) 
if cpuGuess == numberToGuess: 
print("\nCPU guess",x,":",cpuGuess,"\033[33mWinner Winner Chicken Dinner!\033[0m") 
cpu = cpu + 1 
winner = 1 
break 
elif cpuGuess > numberToGuess:
print("CPU guess",x,":",cpuGuess,"Too High")
elif cpuGuess < numberToGuess:
print("CPU guess",x,":",cpuGuess,"Too Low")

print("\n\033[32mScore Board")
print("********** ********** **********")
print("Player", player)
print("CPU", cpu)
print("********** ********** **********\033[0m")

while True:
play = input("\nWould you like to play again? (y or n) ")
if play == "y" or play == "n":
break
else:
print("Invalid input. Please enter 'y' or 'n'.")

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.