【问题标题】:Trying to create a dice simulation game with two rolls and a returned value试图创建一个骰子模拟游戏,有两个掷骰和一个返回值
【发布时间】:2021-12-04 13:26:02
【问题描述】:

我正在设计一个调用随机掷骰子函数的函数。如果两个数字都猜对了,钱就会翻三倍。如果不是,钱就会丢失。如果是,则掷出的骰子的总和是失败的目标。如果您在滚动guess1 或guess2 之前滚动失败的目标,则您输了,如果没有,您将赢钱并加倍...

我正在努力做到这一点,以便掷骰子的顺序无关紧要,也不会重复计算数字。

我也在努力为失去目标的情况制作一个while循环......请帮助!对此非常陌生。

def play(guess1:int,guess2:int, dollars:int):
    
    random_die1 = roll_one_die() ##calling helper function 
    random_die2 = roll_one_die() 
    win_count = 0 ## these are not correct, not sure how to properly account for different rolls
    if guess1 == random_die1:
        win_count = 1
    else:
        win_count = 0
    
    if guess1 == random_die2:
        if win_count==1:
            win_count = 1
        else:
            win_count = 1
               
    if guess2 == random_die1:
        if guess1 != random_die1 and guess1 != random_die2:
            win_count+= 1
    else:
        win_count += 0
    if guess2 == random_die2:
        if guess2 != random_die1 and guess2 != random_die2:
            win_count+= 1
        #if win_count==2:
            #win_count=2
        else:
            win_count +=0 

    if win_count ==2:
        dollars=dollars*3
    elif win_count ==0:
        dollars=0
    else:
        losing_target= random_die1+random_die2
        new_roll1 = roll_one_die()
        new_roll2 = roll_one_die()
        sum_of = (new_roll1 + new_roll2)
        if losing_target==sum_of:
            dollars==0        
        while sum_of!= losing_target: ## not sure how to properly loop so first situation = win or loss
            sum_of == new_roll1 + new_roll2
            if new_roll1 == guess1 or new_roll1 == guess2 or new_roll2 == guess1 or new_roll2 == guess2:
                dollars=dollars*2
        return(dollars)

【问题讨论】:

  • "如果您在滚动最后一次猜测之前滚动失败的目标..." 最后的猜测是什么?有guess1和guess2。为什么会有一个while循环?失去目标的情况需要进一步解释。
  • 感谢@MikeM 希望更新后的描述更有意义。我不确定用什么来代替 while 循环......因为你一直在滚动直到一个事件首先发生?
  • 谢谢@MikeM 你知道对于win_count,我唯一能做的就是做一堆if/else 语句,还是有更少冗余的方法?

标签: python for-loop if-statement while-loop dice


【解决方案1】:

我不愿意只发布一个完整的解决方案,因为我相信在你自己解决之前,你最好努力解决这个问题。

对于失去目标的情况,当然你需要在while循环中掷骰子,例如

while True:
    new_roll1 = roll_one_die()
    new_roll2 = roll_one_die()
    sum_of = new_roll1 + new_roll2
    if losing_target == sum_of:
        dollars = 0
        break        
    if new_roll1 == guess1 or new_roll1 == guess2 or new_roll2 == guess1 or new_roll2 == guess2:
        dollars = dollars * 2
        break

未经测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2011-01-19
    • 2014-03-16
    • 2013-11-28
    • 1970-01-01
    • 2012-02-29
    • 2016-02-07
    相关资源
    最近更新 更多