我正在設計一個函式來呼叫隨機擲骰子的函式。如果兩個數字都猜對了,錢就會翻三倍。如果不是,錢就虧了。如果是,則擲出的骰子總和就是失敗的目標。如果你在擲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)
uj5u.com熱心網友回復:
我不愿意只發布一個完整的解決方案,因為我相信在您自己解決之前,您最好為此奮斗。
對于丟失目標的情況,您肯定需要在 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
未經測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/322691.html
標籤:Python 循环 if 语句 while 循环 骰子
上一篇:如何僅列印來自foriinrange回圈的輸入之一?
下一篇:獲得陣列和陳述句的最小值?
