我想运行我的代码,但是,我不断收到错误消息。 这是关于一个表单字段,在其中输入其值。如果该值在特定范围内,则将获得1欧元。代码是:
class Guess(Page):
form_model = 'player'
form_fields = ['guess']
def is_displayed(self):
return self.round_number == 2
def vars_for_template(self):
if self.player.guess >= 11.25 and self.player.guess <= 13.75:
self.player.cumulative_guthaben = self.player.cumulative_guthaben + 1
else:
self.player.cumulative_guthaben = self.player.cumulative_guthaben
return {
'current_credit': self.player.cumulative_guthaben,
'anzahlspieler': Constants.number_of_players,
'round_number': self.round_number,}
我确实知道这是一个类型错误,但是我没有成功解决它
Here is a shorter Version of my Class:
class Player(BasePlayer):
guthaben = models.CurrencyField(initial=c(0))
cumulative_guthaben = models.CurrencyField()
donation = models.FloatField(min=c(0))
earnedmoney = models.CurrencyField()
frequency = models.StringField()
rival = models.StringField()
guess = models.FloatField(label="Your Guess:")
答案 0 :(得分:1)
您会收到错误,因为在某个时刻self.player.guess
是NoneType,它应该是浮点数才能将其与浮点数进行比较。您可以使用isinstance(i, type)
。您不需要其他,因为您无需更改self.player.cumulative_guthaben
的值。
但是您应该检查其余的代码,为什么self.player.guess
是NoneType。
像这样:
class Guess(Page):
form_model = 'player'
form_fields = ['guess']
def is_displayed(self):
return self.round_number == 2
def vars_for_template(self):
if isinstance(self.player.guess, float):
if (self.player.guess >= 11.25 and self.player.guess <= 13.75):
self.player.cumulative_guthaben = self.player.cumulative_guthaben + 1
return {
'current_credit': self.player.cumulative_guthaben,
'anzahlspieler': Constants.number_of_players,
'round_number': self.round_number,}