jeudi 1 octobre 2020

Learning Python - Number guess game (machine is guessing)

I'm new to Python. I'm trying to write a small game that asks the end user to pick a number from 1 to 1000 and keep it in their head (the number is not provided to the program). The program should be able to find the number within 10 guesses. As I typically do, I went down the wrong path. My program works most of the time, but there are occasions where it does not find the number in under 10 guesses. Here is my code:

# script to guess a user's number between 1 and 1000 within 10 guesses

# import random so we can use it to generate random numbers
from random import randint

# Variables
lowerBound = 1
upperBound = 1000
numGuesses = 1
myGuess = 500
failed = False

# Welcome Message
print("#####################################################################################################"
      "\n#                                                                                                   #"
      "\n#   Please think of a number between 1 and 1000.  I will attempt to guess the number in 10 tries.   #"
      "\n#                                                                                                   #"
      "\n#####################################################################################################")

while numGuesses <= 10:

    # if the lower and upper bounds match we've found the number
    if lowerBound == upperBound:
        print(f"\nYour number is {str(lowerBound)}.  It took me '{str(numGuesses)} guesses!")
        break

    print(f"\nIs the number {str(myGuess)}?  If correct, type CORRECT.  If low, type LOW.  If high, type HIGH.")
    # uncomment for var output
    # print(f"\nGuesses = {str(numGuesses)}\nLower bound = {str(lowerBound)}\nUpper bound = {str(upperBound)}")
    userFeedback = input("\nResponse: ").upper()

    if userFeedback == 'HIGH':
        print(f"\nGuess #{str(numGuesses)} was too high")
        if numGuesses == 10:
            failed = True
            break
        upperBound = myGuess - 1
        myGuess = randint(lowerBound, upperBound)
    elif userFeedback == 'LOW':
        print(f"\nGuess #{str(numGuesses)} was too low")
        if numGuesses == 10:
            failed = True
            break
        lowerBound = myGuess + 1
        myGuess = randint(lowerBound, upperBound)
    elif userFeedback == 'CORRECT':
        print(f"\nYour number is {str(myGuess)}!  It took me {str(numGuesses)} guesses!")
        break

    numGuesses += 1

if failed:
    print(f"\nMy final guess of {str(myGuess)} was not correct.  I wasn't able to guess your number in 10 tries.")

It seems clear (now) that the way I'm whittling down the numbers is not going to work. I had originally thought to ask if it was 500 and, if lower, ask if it was 250. If lower again, ask if it was 125, and so on. If higher, ask if it was 750, 875 and so on. Is that the correct approach here?

I've been thinking about this too long and I believe I've cooked my brain. Thanks!




Aucun commentaire:

Enregistrer un commentaire