Simple while loop, not working as expected. I am trying to create a function that will simulate a roll of a die, and keep a running total of the result, until that total is >= m, at which point it should stop. I want to know what the final total is, and how many rolls it took to get there.
Currently it rolls twice, and reports a sum of 9. I have checked the code outside the loop and it does what it should do (that is, these 3 lines: r = rdm.randint(1,6), tot += r, rolls.append(r)).
What am I missing??
def roll(m):
rolls = []
tot = 0
while tot < m:
r = rdm.randint(1,6)
tot += r
rolls.append(r)
return tot
return rolls
return r
m=100
roll(m)
print "The number of rolls was", len(rolls)
print "The total is", tot
returnstatement will return code. Also not accepting return valueroll(m)NameErrorsincerollsdoesn't exist outside the function?