Search code examples
pythonnameerror

Can't figure out Python 3.7 NameError


everyone! So, I tried to code a simple guessing game, but I keep getting the same error "name 'upperBound' is not defined", and can't figure out how to fix it. I will attach a screenshot of an error as well.

import math

lowerNum = int(input("Enter lower point: "))
upperNum = int(input("Enter upper point: "))

guesses = round(math.log(upperBound))

print("Number of guesses: ", guesses)

response = ""
count = 0

while response != "y" and count < guesses:
    compGuess = round((lowerBound + upperBound)/2)
    print ("Is your number: ", compGuess)
    count += 1
    response = input("either h if too high, l if too low, y if it's correct: ")
    if response == 'h':
        upperBound = compGuess
    elif response == 'l':
        lowerBound = compGuess
    elif response == 'y':
        print ("The computer guessed your answer in", count, "guesses.")
    else:
        print ("Invalid input" , end= '--')
        count -= 1
    compGuess = round((lowerBound + upperBound) /2)
if guesses <= 0:
    print("Computer has used up all the guesses : (")
else:
    pass

error screenshoot


Solution

  • Line 3 defines:

    upperNum = int(input("Enter upper point: "))
    

    Yet line 6 uses upperBound:

    guesses = round(math.log(upperBound))