[英]How can I only make a certain condition run once in a method?
我只希望第一次运行该方法时val为0,但我找不到解决方法。 第一次运行该方法时,滚动值等于该值,然后用户必须滚动值加一。 如果用户不滚动下一个值,则显示“您滚动了{}但需要滚动{}”。 如果他们得到正确的掷骰,那么'你知道了! 下一个值为{}'。
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)
使它成为对象属性并在构造函数中进行设置。
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.