简体   繁体   English

如何只使某个条件在方法中运行一次?

[英]How can I only make a certain condition run once in a method?

I only want val to be 0 the first time I run the method but I can't figure out a way to do this. 我只希望第一次运行该方法时val为0,但我找不到解决方法。 The first time the method is ran the value rolled equals the value, then the user has to roll the value plus one. 第一次运行该方法时,滚动值等于该值,然后用户必须滚动值加一。 If the user does not roll the next value, 'You rolled a {} but needed to roll a {}' is displayed. 如果用户不滚动下一个值,则显示“您滚动了{}但需要滚动{}”。 If they get the correct roll then 'You got it! 如果他们得到正确的掷骰,那么'你知道了! Next value is {}' will be displayed. 下一个值为{}'。

def roll(self):
        'primary game play'
        ds.shake()
        rolled = ds.getTotalRoll()
        while True:
            val = 0
            if val == 0:
                rolled = val
                val += 1
                return 'You got it! Next value is {}'.format(val)
                break
        if rolled == val:
            val +=1
            self.count += 1
            return 'You got it! Next value is {}'.format(val)
        else:
            self.count += 1
            return 'You rolled a {} but needed to roll a {}'.format(rolled, val)

Make it an object property and set it in the constructor. 使它成为对象属性并在构造函数中进行设置。

def __init__(self):
    self.val = 0
    ...

def roll(self):
    'primary game play'
    ds.shake()
    rolled = ds.getTotalRoll()
    while True:
        if self.val == 0:
            rolled = self.val
            self.val += 1
            return 'You got it! Next value is {}'.format(self.val)
            break
    if rolled == self.val:
        self.val +=1
        self.count += 1
        return 'You got it! Next value is {}'.format(self.val)
    else:
        self.count += 1
        return 'You rolled a {} but needed to roll a {}'.format(rolled, self.val)

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

相关问题 我如何才能团结一致地只找到一次我的测试方法? - How can I make unitest to find my single test method only once? 如何制作只能触发一次的命令? - How can I make a command that can only be triggered once? 每次我运行 pyinstaller 可执行文件时,pyngrok 都会下载 ngrok。 我怎样才能让它只下载一次 ngrok? - pyngrok downloads ngrok each time i run the pyinstaller executable. How can i make it download ngrok only once? 如何在 django 中只运行一次 celery 任务? - how can I run celery task only once in django? 如何使 `else` 块在此代码中仅出现一次? - How can I make the `else` block only occur once in this code? 我该如何创建一个类,该类从python中的另一个类继承方法,但又仅更改某个方法的某些部分? - How can I make a class that inherits methods from another class in python but also change only some part of a certain method? 为什么我不能在我的方法中只运行一行? - Why can't I run a single line in my method only once? 如何检查某个条件并参考 pygame 执行一次 - How do I check for a certain condition and implement it once with reference to pygame 如何让python在模板中只输入一次if条件 - How to make python in template to enter if condition only once 每天只运行一次的方法 - Method to run only once a day
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM