Python 如何计算调用此函数的次数?

Python 如何计算调用此函数的次数?,python,python-3.x,Python,Python 3.x,因此,我的任务(我必须使用while语句)是做一个数字猜测游戏,其中一部分是显示玩家在猜对数字后的猜测次数。我发现我读过的东西应该有用,但没有用。这是我的密码 #A text program that is a simple number guessing game. import time import random #Setting up the A.I. Number = random.randint(1,101) def AI(): B = AI.counter =+ 1

因此,我的任务(我必须使用while语句)是做一个数字猜测游戏,其中一部分是显示玩家在猜对数字后的猜测次数。我发现我读过的东西应该有用,但没有用。这是我的密码

#A text program that is a simple number guessing game.
import time
import random
#Setting up the A.I.
Number = random.randint(1,101)
def AI():
    B = AI.counter =+ 1
    Guess = int(input("Can you guess what number I'm Thinking of?: "))
    while Guess > Number:
        print("nope, to high.")
        return AI()
    while Guess < Number:
        print("Sorry, thats to low. try again!")
        return AI()
    while Guess == Number:
        print("Congragulations! you win! You guessed " + str(B) + " times")
        time.sleep(60)
        quit()
AI.counter = 0
AI()
#一个简单的数字猜测游戏的文本程序。
导入时间
随机输入
#建立人工智能。
Number=random.randint(1101)
定义AI():
B=AI。计数器=+1
Guess=int(输入(“你能猜出我在想什么数字吗?:”)
猜测>数字时:
打印(“不,高。”)
返回AI()
而猜测<数字:
打印(“对不起,太低了。再试一次!”)
返回AI()
而Guess==数字:
打印(“恭喜!你赢了!你猜对了”+str(B)+“次”)
时间。睡眠(60)
退出
AI.counter=0
AI()
虽然当玩家得到正确的数字时,它会说玩家一猜就得到了,即使不是这样。

使用默认参数:

def AI(B=1):
    Guess = int(input("Can you guess what number I'm Thinking of?: "))
    while Guess > Number:
        print("nope, to high.")
        return AI(B + 1)
    while Guess < Number:
        print("Sorry, thats to low. try again!")
        return AI(B + 1)
    while Guess == Number:
        print("Congragulations! you win! You guessed " + str(B) + " times")
        time.sleep(60)
        return
另外,这里应该使用
if
s而不是
while
s,因为循环只运行一次,但是
while
s也可以工作,所以这很好。此外,递归可能会耗尽RAM,这只会浪费资源,前提是您可以实现与循环相同的东西,但您的方法也可以,所以这也很好。

使用默认参数:

def AI(B=1):
    Guess = int(input("Can you guess what number I'm Thinking of?: "))
    while Guess > Number:
        print("nope, to high.")
        return AI(B + 1)
    while Guess < Number:
        print("Sorry, thats to low. try again!")
        return AI(B + 1)
    while Guess == Number:
        print("Congragulations! you win! You guessed " + str(B) + " times")
        time.sleep(60)
        return

另外,这里应该使用
if
s而不是
while
s,因为循环只运行一次,但是
while
s也可以工作,所以这很好。此外,递归可能会耗尽您的RAM,这只会浪费资源,前提是您可以实现与循环相同的东西,但您的方法也可以,所以这也很好。

您非常接近@Simpson!这里有一个稍加修改的版本,它应该能满足您的需求:)

如果你有任何问题,请告诉我

#A text program that is a simple number guessing game.
import random
#Setting up the A.I.
def AI():
    counter = 1
    number = random.randint(1,101)
    guess = int(input("Can you guess what number I'm Thinking of?: "))
    while guess != number:
        if guess < number:
            print("Sorry, thats to low. try again!")
        else:
            print("nope, too high")
        counter += 1
        guess = int(input("Can you guess what number I'm Thinking of?: "))

    print("Congragulations! you win! You guessed " + str(counter) + " times")
    time.sleep(60)
    quit()

AI()
#一个简单的数字猜测游戏的文本程序。
随机输入
#建立人工智能。
定义AI():
计数器=1
number=random.randint(1101)
guess=int(输入(“你能猜出我在想什么数字吗?:”)
猜猜看!=编号:
如果猜测<数字:
打印(“对不起,太低了。再试一次!”)
其他:
打印(“不,太高”)
计数器+=1
guess=int(输入(“你能猜出我在想什么数字吗?:”)
打印(“恭喜!你赢了!你猜对了”+str(计数器)+“次数”)
时间。睡眠(60)
退出
AI()

你很接近@Simpson!这里有一个稍加修改的版本,它应该能满足您的需求:)

如果你有任何问题,请告诉我

#A text program that is a simple number guessing game.
import random
#Setting up the A.I.
def AI():
    counter = 1
    number = random.randint(1,101)
    guess = int(input("Can you guess what number I'm Thinking of?: "))
    while guess != number:
        if guess < number:
            print("Sorry, thats to low. try again!")
        else:
            print("nope, too high")
        counter += 1
        guess = int(input("Can you guess what number I'm Thinking of?: "))

    print("Congragulations! you win! You guessed " + str(counter) + " times")
    time.sleep(60)
    quit()

AI()
#一个简单的数字猜测游戏的文本程序。
随机输入
#建立人工智能。
定义AI():
计数器=1
number=random.randint(1101)
guess=int(输入(“你能猜出我在想什么数字吗?:”)
猜猜看!=编号:
如果猜测<数字:
打印(“对不起,太低了。再试一次!”)
其他:
打印(“不,太高”)
计数器+=1
guess=int(输入(“你能猜出我在想什么数字吗?:”)
打印(“恭喜!你赢了!你猜对了”+str(计数器)+“次数”)
时间。睡眠(60)
退出
AI()

无递归-将whiles更改为ifs,并在方法中添加计数器

import time
import random

Number = random.randint(1,101)

def AI():
    B = 1
    Guess = int(input("Can you guess what number I'm Thinking of?: "))
    while True:
        if Guess > Number:
            print("nope, to high.")
        elif Guess < Number:
            print("Sorry, thats to low. try again!")

        if Guess == Number:
            print("Congragulations! you win! You guessed " + str(B) + " times")
            time.sleep(2)
            break # leave the while true

        # increment number
        B += 1   
        Guess = int(input("Can you guess what number I'm Thinking of?: "))
AI()
导入时间
随机输入
Number=random.randint(1101)
定义AI():
B=1
Guess=int(输入(“你能猜出我在想什么数字吗?:”)
尽管如此:
如果猜测>数字:
打印(“不,高。”)
elif Guess<数字:
打印(“对不起,太低了。再试一次!”)
如果Guess==数字:
打印(“恭喜!你赢了!你猜对了”+str(B)+“次”)
时间。睡眠(2)
打破,保持原样
#增量数
B+=1
Guess=int(输入(“你能猜出我在想什么数字吗?:”)
AI()

无递归-将whiles更改为ifs,并在方法中添加计数器

import time
import random

Number = random.randint(1,101)

def AI():
    B = 1
    Guess = int(input("Can you guess what number I'm Thinking of?: "))
    while True:
        if Guess > Number:
            print("nope, to high.")
        elif Guess < Number:
            print("Sorry, thats to low. try again!")

        if Guess == Number:
            print("Congragulations! you win! You guessed " + str(B) + " times")
            time.sleep(2)
            break # leave the while true

        # increment number
        B += 1   
        Guess = int(input("Can you guess what number I'm Thinking of?: "))
AI()
导入时间
随机输入
Number=random.randint(1101)
定义AI():
B=1
Guess=int(输入(“你能猜出我在想什么数字吗?:”)
尽管如此:
如果猜测>数字:
打印(“不,高。”)
elif Guess<数字:
打印(“对不起,太低了。再试一次!”)
如果Guess==数字:
打印(“恭喜!你赢了!你猜对了”+str(B)+“次”)
时间。睡眠(2)
打破,保持原样
#增量数
B+=1
Guess=int(输入(“你能猜出我在想什么数字吗?:”)
AI()

计数函数调用是Python的一个非常有用的功能。您可以将装饰器定义为:

def call_counted(funct):
    def wrapped(*args, **kwargs):
        wrapped.count += 1  # increase on each call
        return funct(*args, **kwargs)
    wrapped.count = 0  # keep the counter on the function itself
    return wrapped
然后,您可以使用它来装饰一个您希望计算调用次数的函数,而无需处理进程流中的计数器本身:

import time
import random

secret_number = random.randint(1, 101)

@call_counted  # decorate your AI function with the aforementioned call_counted
def AI():
    current_guess = int(input("Can you guess what number I'm thinking of?: "))
    while current_guess > secret_number:
        print("Nope, too high. Try again!")
        return AI()
    while current_guess < secret_number:
        print("Sorry, that's too low. Try again!")
        return AI()
    while current_guess == secret_number:
        print("Congratulations! You win! You guessed {} times.".format(AI.count))
        time.sleep(60)
        quit()

AI()
导入时间
随机输入
secret_number=random.randint(1101)
@call_counted#用前面提到的call_counted装饰您的AI功能
定义AI():
current_guess=int(输入(“你能猜出我在想什么数字吗?:”)
当前\u guess>秘密\u编号时:
打印(“不,太高。再试一次!”)
返回AI()
当前猜数<秘密猜数:
打印(“对不起,太低了。再试一次!”)
返回AI()
当当前_guess==秘密_编号时:
打印(“祝贺你!你赢了!你猜了{}次。”.format(AI.count))
时间。睡眠(60)
退出
AI()
我重新设计了你的代码,但本质上是一样的

我要避免递归,tho,因为它可以编写得更简单,并且不需要计算函数调用:

import time
import random

secret_number = random.randint(1, 101)

def AI():
    counter = 0
    while True:
        counter += 1
        current_guess = int(input("Can you guess what number I'm thinking of?: "))
        if current_guess > secret_number:
            print("Nope, too high. Try again!")
        elif current_guess < secret_number:
            print("Sorry, that's too low. Try again!")
        else:
            break
    print("Congratulations! You win! You guessed {} times.".format(counter))
    time.sleep(60)
    quit()

AI()
导入时间
随机输入
secret_number=random.randint(1101)
定义AI():
计数器=0
尽管如此:
计数器+=1