擲骰6骰子(也稱爲D6),直到它們都出現'1'。計算所花費的卷數。 運行100個這樣的試驗。打印每卷的結果並報告所需的平均卷數。確定在一對骰子上獲得1-1的擲骰的平均數
使用嵌套循環。外循環運行100次試驗;內循環繼續滾動,直到出現1-1。然後更新運行計數並轉到下一個試用版。
import random
dice1, dice2 = " ", " "
roll = " "
for roll in range(1, 101):
roll = 0
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
print(dice1, ",", dice2)
while dice1 == 1 and dice2 == 1:
break
時2 1的捲起這並不能阻止我需要幫助積累卷號和試驗序號
'break'只有在'while'循環中有效。我假設你想要一個if語句。 –
爲什麼你初始化變量爲字符串,如果你稍後分配給他們的數字? – Barmar
一旦爲你解決這個問題,請記住「接受」你最喜歡的答案。這正確地關閉了StackOverflow的問題。 – Prune