2015-08-14 39 views
-4

我已經寫了一個骰子滾輪,生成隨機數並將它們插入到列表中。然後它在列表中打印每個迭代。重複函數不適用於列表Python

這是第一次運行。但是,當被問及「你想重新註冊嗎?」時並選擇'y'我得到一個StopIteration錯誤

不應該Python只是覆蓋列表?

編輯:添加更多的代碼

File "./test.py", line 159, in diceRoller 
    sname = it1.next() 
StopIteration 




#imports 

import random 


#Stats start at 0 

acc = 0 
com = 0 
con = 0 
dex = 0 
fig = 0 
inl = 0 
per = 0 
str = 0 
wil = 0 


sname = 0 
statList = ['Acc', 'Com', 'Con', 'Dex', 'Fig', 'Int', 'Per', 'Str', 'Wil'] 
it1 = iter(statList) 



stat = [] 

def diceRoller(): 


    rollcount = 1 
    index = 0 
    print ' ' 
    print "Rolling 3 d6's for each stat" 

    print fmtt.format(' ', 'Stat','Your rolls', 'Total', 'Stat value') 

    while rollcount < 10: 

    die1 = random.randint(1,6) 
    die2 = random.randint(1,6) 
    die3 = random.randint(1,6) 
    total = sum([die1,die2,die3]) 

    rollcount += 1 


(removed large if block - that sets bonus value) 
    bonus = 4 


for item in range(1): 
    stat.append(bonus) 

#print stat 


#for i in range(1): 
    sname = it1.next() 


print fmt.format(' ', sname, die1, die2, die3, total, bonus) 



    else: 
    rroll = raw_input("Do you want to reroll? (y/n)") 
    if rroll == 'y': 
     diceRoller() 
    else: 
     statCalc()  

#### END DEF diceRoller 
+2

那麼是什麼在此代碼是'it1' ? –

+0

你是否也指「再次調用函數」之類的東西?因爲這裏沒有'repeat()'。 (我認爲你的標題可能會使用['itertools.repeat'](https://docs.python.org/2/library/itertools.html#itertools.repeat)或['numpy.repeat'](http ://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html)) –

+0

這是當迭代器用完值時應該拋出的異常。沒有足夠的信息在這裏回答你的問題。 – rocktheartsm4l

回答

0

發送statList到diceRoller函數作爲參數,然後把

it1 = iter(statList) 

該函數內部

+0

謝謝!這比我預期的要容易得多。 – Ippy