简体   繁体   English

如何在Python中放入“如果函数已被多次调用”?

[英]How to put “if the function has been called this many times” in Python?

So I'm designing a hangman game using Python and Kivy and I want to add a win/lose option. 所以我正在设计一个使用Python和Kivy的子手游戏,我想添加一个胜利/失败选项。

one of the functions I've defined is Button_pressed which hides the button if it's been pressed but I want the function man_is_hung() to have something that says "if the button has been pressed 6 times, show "game over"." 我定义的函数之一是Button_pressed,如果按下了按钮,它将隐藏按钮,但是我希望函数man_is_hung()的内容为“如果按下按钮6次,则显示”游戏结束”。

Would someone please help me? 有人可以帮我吗?

 def button_pressed(button):
        for (letter, label) in CurrentWord:
            if (letter.upper() == button.text): label.text=letter 
        button.text=" " # hide the letter to indicate it's been tried

def man_is_hung():
    if button_pressed(button)

Use a decorator : 使用装饰器

Example: 例:

class count_calls(object):
    def __init__(self, func):
        self.count = 0
        self.func = func
    def __call__(self, *args, **kwargs):
        # if self.count == 6 : do something
        self.count += 1
        return self.func(*args, **kwargs)

@count_calls
def func(x, y):
    return x + y

Demo: 演示:

>>> for _ in range(4): func(0, 0)
>>> func.count
4
>>> func(0, 0)
0
>>> func.count
5

In py3.x you can use nonlocal to achieve the same thing using a function instead of a class: 在py3.x你可以使用nonlocal使用功能,而不是一个类来实现同样的事情:

def count_calls(func):
    count = 0
    def wrapper(*args, **kwargs):
        nonlocal count
        if count == 6:
            raise TypeError('Enough button pressing')
        count += 1
        return func(*args, **kwargs)
    return wrapper

@count_calls
def func(x, y):
    return x + y

Demo: 演示:

>>> for _ in range(6):func(1,1)
>>> func(1, 1)
    ...
    raise TypeError('Enough button pressing')
TypeError: Enough button pressing

Here's a way to have static variables in functions that doesn't involve globals or classes: 这是在不涉及全局变量或类的函数中具有静态变量的方法:

def foobar():
    foobar.counter = getattr(foobar, 'counter', 0)
    foobar.counter += 1
    return foobar.counter

for i in range(5):
    print foobar()

You could store the button as a class like so: 您可以将按钮存储为如下所示的类:

class button_pressed(Object):
    def __init__(self):
        self.num_calls = 0

    def __call__(self, button):
        self.num_calls += 1
        if self.num_calls > 6:
            print "Game over."
            return None
        else:
           # Your regular function stuff goes here.

This is basically a manual decorator, and while it might be a bit complicated for what you are trying to do this is an easy way to do bookkeeping on a function. 这基本上是一个手动装饰器,尽管对于您尝试执行的操作可能有点复杂,但这是对函数进行簿记的一种简便方法。

Really, the correct way to do this kind of thing is to use a decorator that takes a parameter for the number of times you want the function to be able to be called and then applies the above pattern automatically. 的确,执行此操作的正确方法是使用装饰器,该装饰器接受您希望函数能够被调用的次数的参数,然后自动应用上述模式。

Edit: Ahh! 编辑:啊! hcwhsa beat me to it. hcwhsa击败了我。 His solution is the more general one I was talking about above. 他的解决方案是我上面所说的更通用的解决方案。

ummmm

num_presses = 0
def button_pressed(button):
    global num_presses
    num_presses += 1
    if num_presses > X:
         print "YOU LOSE SUCKA!!!"
    for (letter, label) in CurrentWord:
        if (letter.upper() == button.text): label.text=letter 
    button.text=" " # hide the letter to indicate it's been tried

would be one way of doing it ... Im kind of suprised you have made it this far without knowing how to save simple states. 我会感到惊讶的是,您在不知道如何保存简单状态的情况下走了这么远。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何测试一个函数被调用了多少次 - How to test how many times a function has been called 计算一个函数被调用了多少次 - Count how many times a function has been called 跟踪在 Python 中调用了递归 function 的次数 - Keep track of how many times a recursive function has been called in Python 为游戏中的功能插入计数器。 (在整个运行过程中调用了多少次 function) - Insert counter for functions in game. (How many times has a function been called throughout the whole run) 如何计算通过unittest调用内部类方法的次数? - How to count how many times a internal class method has been called through unittest? 计算递归调用 function 的次数(Python) - Counting how many times a function is called recursively (Python) 如何判断是否已在Python中调用过函数? - How do I tell whether a function has been called in Python? Python:如何等到 function 在不同的线程中被调用? - Python: How to wait until a function has been called in a different thread? Python递归函数调用次数过多 - Python recursive function called too many times for循环声明中的函数是否会在Python中多次调用? - Is a function in a for loop declaration will be called for many times in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM