所以问题出在9到32行。它输出说“ 3 0 0”,然后是“ 3 4 0”,然后是“ 3 4 6”。而是应该说“ 3 4 6”,然后可以是“ 6 1 2”。我知道这与numRolled变量以及该变量的循环方式有关,但我想不起来还要把它放在哪里。
我尝试将numRolled = 0放在“ for i in range(3)”的末尾,但这只能使第一个数字发生变化。我曾尝试将数字放入单个变量列表中,但我对列表编码并不十分有信心,因此我决定继续使用此处的内容。
def DiceGame():
numRolled = 1
RunScore = 0
Roll1 = 0
Roll2 = 0
Roll3 = 0
#rolls the 3 dice 100 times
for x in range(100):
numRolled = 0
#rolls the 3 dice
for i in range(3):
score = rdm.randint(1,6)
numRolled += 1
#assigns score to each die
if numRolled == 1:
Roll1 = score
if numRolled == 2:
Roll2 = score
if numRolled == 3:
Roll3 = score
if Roll1 == Roll2 and Roll1 == Roll3:
RunScore += 100
else:
DiceTot = Roll1 + Roll2 + Roll3
# If Total of Die MOD 2 == 0 then Running score += Dice Total
if DiceTot % 2 == 0:
RunScore += DiceTot
else:
RunScore -= DiceTot
print(Roll1, Roll2, Roll3)
print(RunScore)
如上所述,它输出说“ 3 0 0”然后“ 3 4 0”然后“ 3 4 6”。而是应该说“ 3 4 6”,然后可能是“ 6 1 2”
答案 0 :(得分:1)
更改此
def game:
forlopp:
forloop:
print()
print()
对此
def game:
forlopp:
forloop:
print()
print()
答案 1 :(得分:0)
将与单个骰子无关的所有内容移出第一个循环:
Function Loss(worksheet1 As Worksheet) As Long
Dim min As Double
Dim i As Integer
Dim myRight As Long, Colcount As Long
min = 0
myRight = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
For Colcount = 1 To myRight
If (ActiveSheet.Cells(11, Colcount) > min) And Abs(ActiveSheet.Cells(11, Colcount) <= 100) Then
min = ActiveSheet.Cells(11, Colcount)
End If
Exit For
Next Colcount
End Function
那应该修复代码。
但是,使用列表会更容易。您可以像这样生成3个掷骰子的列表:
for x in range(100):
numRolled = 0
#rolls the 3 dice
for i in range(3):
score = rdm.randint(1,6)
numRolled += 1
#assigns score to each die
if numRolled == 1:
Roll1 = score
if numRolled == 2:
Roll2 = score
if numRolled == 3:
Roll3 = score
####
# Here the code below has been un-indented and removed from the range(3) loop
####
if Roll1 == Roll2 and Roll1 == Roll3:
RunScore += 100
else:
DiceTot = Roll1 + Roll2 + Roll3
#####
# Note: I have indented the following block to put it inside
# this "else" clause, so that it can use "DiceTot" reliably.
#####
# If Total of Die MOD 2 == 0 then Running score += Dice Total
if DiceTot % 2 == 0:
RunScore += DiceTot
else:
RunScore -= DiceTot
print(Roll1, Roll2, Roll3)
print(RunScore)
也可以将其写为列表理解:
rolls = []
for _ in range(3):
rolls.append(rdm.randint(1,6))
无论您做什么,都可以更轻松地生成统计信息:
rolls = [rdm.randint(1,6) for _ in range(3)]
您可以使用if all(roll == rolls[0] for roll in rolls):
RunScore += 100
else:
DiceTot = sum(rolls)
if DiceTot % 2 == 0:
RunScore += DiceTot
else:
RunScore -= DiceTot
函数进行打印:
join
使用这样的列表将使您摆脱3个骰子掷骰子变量,并无需更改任何内容即可随意更改骰子掷骰子次数。