掷骰子程序

编程入门 行业动态 更新时间:2024-10-17 21:21:55
骰子程序 - TypeError:列表索引必须是整数,而不是列表(Roll dice program - TypeError: list indices must be integers, not list)

我正在编写一个程序,可以滚动任意数量的骰子,任意次数。 我陷入了多次掷骰子的功能,然后用滚动替换列表中的零。 我继续在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 L

I'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] += 1

rollDice返回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] += 1

Your 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.

更多推荐

本文发布于:2023-08-07 01:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:骰子   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!