【问题标题】:essentially same code,but different result基本相同的代码,但不同的结果
【发布时间】:2022-08-05 22:24:47
【问题描述】:

这是UCB CS61A的第一个项目猪的问题5

这是错误的代码(我的代码)

while(score0<goal and score1<goal):
    if who==0 :
        cur_strategy = strategy0
        cur_score = score0
        cur_op_score=score1
        cur_op_strategy = strategy1
    else:
        cur_strategy = strategy1
        cur_score = score1
        cur_op_score=score0
        cur_op_strategy = strategy0
    cur_score=cur_score+take_turn(cur_strategy(cur_score,cur_op_score),cur_op_score,dice)

    if(extra_turn(cur_score,cur_op_score)==False):
        who=other(who)

这是正确的代码(我已经测试过了)

while score0 < goal and score1 < goal:
        if who == 0:
            num_rolls = strategy0(score0, score1)
            score0 += take_turn(num_rolls, score1, dice)
            who = other(who) if extra_turn(score0, score1) == False else who
        else:
            num_rolls = strategy1(score1, score0)
            score1 += take_turn(num_rolls, score0, dice)
            who = other(who) if extra_turn(score1, score0) == False else who

但实际上,我认为这两个代码本质上是相同的。

我不知道这是否是问题(来自项目的报价)

每回合只调用一次策略函数(或冒着破坏 GUI 的风险)。

    标签: function


    【解决方案1】:

    我认为这是因为为了更新 score0 和 score1 变量,您定义了 cur_score 并改为更新它。问题是 cur_score 只是您要更新的分数的副本,它不是同一个对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 2020-12-15
      • 2020-02-14
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      相关资源
      最近更新 更多