簡體   English   中英

如果條件不工作但其他條件工作

[英]if condition doesn't work but else condition works

當我在 python 控制台中運行以下代碼時,它按預期工作。 但是,當我在 oree 中運行它時,只考慮了 else 條件。 因此,即使滿足條件,我也只會得到值為 3.85 和 0.1(而不是 2 或 1.6)的結果。

for k in range(1,11):
    locals()['choice' + str(k)] = random.randint(0,1)
    locals()['resultHL' + str(k)] = []
win = []

for i in range(1, 11):
    choice = 0
    exec(f'choice+=choice{i}')
    rnum = random.random()
    probability = i / 10
    if rnum <= probability:
        win.append(1)
        if choice == 1:
            exec(f'resultHL{i} = 2')
        else:
            exec(f'resultHL{i} = 3.85')
    else:
        win.append(0)
        if choice == 1:
            exec(f'resultHL{i} = 1.6')
        else:
            exec(f'resultHL{i} = 0.1')

由於 oTree 中的語法,我必須以特定方式設置變量。

class Player(BasePlayer):
    number_entered = models.FloatField(min=0, max=100)
    result_risk = models.FloatField()
    win_risk = models.BooleanField()
    for k in range(1,11):
        locals()['choice' + str(k)] = make_booleanfield()
        locals()['probHL' + str(k)] = models.FloatField()
        locals()['winHL' + str (k)] = models.BooleanField()
        locals()['resultHL' + str(k)] = models.FloatField()
    del k

def make_booleanfield():
    return models.BooleanField(
        choices=[[True,'A'],[False,'B'],],
        widget=widgets.RadioSelectHorizontal,
    )


class ResultsHL(Page):
    @staticmethod
    def vars_for_template(player: Player):
        for i in range(1, 11):
            choice = 0
            exec(f'choice+=player.choice{i}')
            rnum = random.random()
            probability = i / 10
            exec(f'player.probHL{i}=probability')
            if rnum <= probability:
                exec(f'player.winHL{i}=1')
                if choice == 1:
                    exec(f'player.resultHL{i} = C.A_win')
                else:
                    exec(f'player.resultHL{i} = C.B_win')
            else:
                exec(f'player.winHL{i}=0')
                if choice == 1:
                    exec(f'player.resultHL{i} = C.A_lose')
                else:
                    exec(f'player.resultHL{i} = C.B_lose')

locals() / exec()的東西可能有一個作用域錯誤——與其嘗試調試它,我建議只使用字典或列表。 這是一個使用三個列表而不是 20 個整數和一個列表的示例,使用存根Player class 來模擬您的用例:

import random

class Player:
    def __init__(self):
        self.result = 0.0


choices = [random.randint(0, 1) for _ in range(10)]
players = [Player() for _ in range(10)]
wins = [int(random.random() <= i / 10) for i in range(1, 11)]

for player, choice, win in zip(players, choices, wins):
    if win:
        if choice:
            player.result = 2
        else:
            player.result = 3.85
    else:
        if choice:
            player.result = 1.6
        else:
            player.result = 0.1


print([player.result for player in players])

打印(例如):

[1.6, 0.1, 1.6, 0.1, 1.6, 0.1, 2, 3.85, 0.1, 2]

我可能還建議通過使用表格來減少if/else的內容:

results = (
    (2.00, 3.85),  # win
    (1.60, 0.10),  # loss
)

for player, choice, win in zip(players, choices, wins):
    player.result = results[win][choice]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM