无法理解Python 3.7 NameError

2024-10-05 14:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

各位!! 所以,我试着编写一个简单的猜测游戏,但我一直得到相同的错误“名称‘上限’未定义”,并且不知道如何修复它。我会附上一个错误的截图以及

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


Tags: inputifresponsecount错误mathintpoint