[英]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.