我正在编写一个程序,可以滚动任意数量的骰子,任意次数。 我陷入了多次掷骰子的功能,然后用滚动替换列表中的零。 我继续在rolls[rv]+=1上收到错误:
TypeError: list indices must be integers, not list
我发送给roll的我的ctrList函数确实返回一个列表,我尝试将该列表更改为我的rollSim函数中的int或str ,但似乎没有任何效果。 也许我会以错误的方式解决这个问题? 这是我到目前为止这个领域的代码:
numDice=int(input("How many dice do you want to roll? ")) numSides=int(input("How many Sides does each have? ")) numRolls=int(input("How many times do you want to roll the dice? ")) def ctrList(numSides, numDice): #Function that seeds list with all zeros L=[] ctr=0 possible= ((numSides * numDice)+1) for p in range (possible): ctr+=1 L.append(0) return L def rollSim(numDice, numSides, numRolls, rollDice): rolls=ctrList(numSides, numDice) for i in range (numRolls): rv= rollDice(numDice, numSides ) rolls[rv]+=1 #where I keep on getting the error at :( return rolls def rollDice(numDice, numSides ): #Function that rolls dice L=[] ctr=0 for dice in range (numDice): L.append (random.randint(1, numSides)) ctr+=1 return LI'm writing a program that will roll any number of dice, with any number of sides, any number of times. I'm stuck in my function that rolls the dice a number of times and then replaces the zeros in the list with the roll. I keep on getting an error on rolls[rv]+=1:
TypeError: list indices must be integers, not list
My ctrList function that I sent to rolls, does return a list and I have tried changing that list into an int or str in my rollSim function, but nothing seems to work. Maybe I am going about this problem in the wrong way? Here is the code I have for this area so far:
numDice=int(input("How many dice do you want to roll? ")) numSides=int(input("How many Sides does each have? ")) numRolls=int(input("How many times do you want to roll the dice? ")) def ctrList(numSides, numDice): #Function that seeds list with all zeros L=[] ctr=0 possible= ((numSides * numDice)+1) for p in range (possible): ctr+=1 L.append(0) return L def rollSim(numDice, numSides, numRolls, rollDice): rolls=ctrList(numSides, numDice) for i in range (numRolls): rv= rollDice(numDice, numSides ) rolls[rv]+=1 #where I keep on getting the error at :( return rolls def rollDice(numDice, numSides ): #Function that rolls dice L=[] ctr=0 for dice in range (numDice): L.append (random.randint(1, numSides)) ctr+=1 return L最满意答案
它似乎而不是:
for i in range (numRolls): rv= rollDice(numDice, numSides ) rolls[rv]+=1 #where I keep on getting the error at :(你需要:
for i in range (numRolls): rv = rollDice(numDice, numSides) for dice in rv: rolls[dice] += 1rollDice返回numDice ( list )的结果。 然后你尝试用另一个列表索引rolls列表,这没有任何意义(这就是错误所说的确切)。
您还可以更改方法以仅为一个骰子返回一个结果:
def rollDice(numSides): return random.randint(1, numSides)因此,您的计数代码版本将起作用。 取决于你想要什么。
It just seems that instead of:
for i in range (numRolls): rv= rollDice(numDice, numSides ) rolls[rv]+=1 #where I keep on getting the error at :(you need:
for i in range (numRolls): rv = rollDice(numDice, numSides) for dice in rv: rolls[dice] += 1Your rollDice returns results for numDice dices (a list). Then you try to index the rolls list with another list, which doesn't make any sense (and that's what the error is saying exactly).
You can also change the method to return only one result, for one dice:
def rollDice(numSides): return random.randint(1, numSides)So your version of counting code will work. Depends on what you want.
更多推荐
发布评论