如何跟踪用户输入的数量,并将其显示在字符串中



我有一个类项目,我正在做一个猜号游戏。我有以下要求:

#1. A main() function that holds the primary algorithm, but itself only passes information among other functions. main() must have the caller for random_int()
#2. A function called in main() (not nested in main()!) that compares the user's guess to the number from random_int() and lets the user know if it was too high or too low.
#3. A function called in main() that asks the user for a new guess.
#4. A function that prints out a string letting the user know that they won.
#5. Tell the user how many guesses it took them to get the correct answer.

我正在努力确定如何跟踪用户在达到正确数字之前的猜测次数,并将该数字显示在字符串中。我当前在函数main():中有一个while循环

def main(random_int, new_guess, user_attempts): #Function that holds the main algorithim and calls all of the functions in the program
r = random_int(size) #Setting parameters to generate a random number between 1 - 1000
n = new_guess() #Assigns the user's input as "guess", and calls the function "new_guess"

while n != r: #While loop to continue until user guesses correct number
if n > r:
print("The number you guessed is too high, guess again.")
elif n < r:         
print("The number you guessed is too low, guess again.")
attempts =+ 1
n = new_guess()

if n == r: #If user guesses correct number, call "win" function
win(random_int, new_guess, user_attempts)

我试图获取attempts的值,并将其存储在函数user_attempts():中,在函数win():中可以调用它。我一直收到错误-TypeError: 'int' object is not callable

上下文的完整程序:

#Python number guessing game
#Import randrange module
from random import randrange
#Initialize variables
attempts = 0
size = 1000
def random_int(size): #Generates a random integer from given parameters (size)
return randrange(1, size+1)
def new_guess(): #Prompts the user to enter an integer as their guess
guess = int(input("Enter your guess (between 1 - 1000): "))
return guess
def user_attempts():
a = attempts
return a
def win(random_int, new_guess, user_attempts): #Prints that the answer is correct, along with the number of guesses it took 
random_int = random_int(size)
a = user_attempts()

if a >= 2: #If it took the user more than 1 attempt, uses "guesses" for proper grammar
print("You guessed the correct number", str(random_int()), ", you win! It took you ", str(user_attempts()), " guesses.")
elif a < 2: #If it took the user only 1 attempt, uses "guess" for proper grammar
print("You guessed the correct number", str(random_int()), ", you win! It took you ", str(user_attempts()), " guess.") 
def main(random_int, new_guess, user_attempts): #Function that holds the main algorithim and calls all of the functions in the program
r = random_int(size) #Setting parameters to generate a random number between 1 - 1000
n = new_guess() #Assigns the user's input as "guess", and calls the function "new_guess"

while n != r: #While loop to continue until user guesses correct number
if n > r:
print("The number you guessed is too high, guess again.")
elif n < r:         
print("The number you guessed is too low, guess again.")
attempts =+ 1
n = new_guess()

if n == r: #If user guesses correct number, call "win" function
win(random_int, new_guess, user_attempts)
main(random_int, new_guess, user_attempts) #Calls the "main" function, runs the program

对于您的"CCD_ 6对象不可调用";错误,这意味着您正在调用一个整数变量,就像它是一个函数一样。我想这就是你所做的:

random_int = random_int(size)

如果random_int是一个函数,那么对它的第一个调用可能会起作用,但随后您将为一个名为random_int的局部变量分配该函数的返回值,该值是一个整数。所以下次调用random_int时,它是一个整数,而不是一个函数。要修复它,除了random_int之外,还应该使用其他变量名,因为这就是您对函数的调用。

+==+

看起来你有:

attempts =+ 1

但你可能是想交换+=:

attempts += 1

attempts =+ 1类似于attempts = +1,其中+是一元正的(类似于一元负的)。

最新更新