Error - ValueError: The number of weights does not match the population in python 3
Error - ValueError: The number of weights does not match the population in python 3
我在 python 中编写了一个代码来制作一个简单的井字游戏,但错误不断出现。我无法弄清楚问题..请帮忙
该代码在第一种情况下工作正常,但我不知道为什么它不是
您可以在任何编辑器上查看代码 -
import random
from random import choices
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
weights = [0.1, 0.05, 0.1, 0.05, 0.3, 0.05 , 0.1, 0.05, 0.1]
_board = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9]
def choose_random():
return(choices(board,weights))
def draw():
print(_board[0], _board[1], _board[2])
print(_board[3], _board[4], _board[5])
print(_board[6], _board[7], _board[8])
def user_chance():
draw()
choice = int(input('Choose any of the block: '))
board.remove(choice)
index = _board.index(choice)
_board[index] = "X"
comp_chance()
def comp_chance():
if(len(board) == 0):
print("It's a tie !")
else:
random = choose_random()
board.remove(random)
index = _board.index(random)
_board[index] = 'O'
user_chance()
user_chance()
在第 17 行中,当您调用 choose_random() 时,棋盘列表和权重列表的长度不匹配,因此您会收到错误消息。
这里有几个问题需要解决:
您正在从 board
列表而不是 weights
列表中删除值,因此您遇到了这个问题
choose_random()
是 return 一个 k
值的列表,你没有指定 k
所以它会 return具有一个值的列表。您应该在 return 语句的末尾添加 [0]
。
您还应该添加检查用户是否在板上输入了有效位置,否则,您将收到 value is not in list
的错误
from random import choices
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
weights = [0.1, 0.05, 0.1, 0.05, 0.3, 0.05 , 0.1, 0.05, 0.1]
_board = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9]
def choose_random():
return(choices(board,weights))[0]
def draw():
print(_board[0], _board[1], _board[2])
print(_board[3], _board[4], _board[5])
print(_board[6], _board[7], _board[8])
def user_chance():
draw()
choice = int(input('Choose any of the block: '))
board.remove(choice)
index = _board.index(choice)
del weights[index]
_board[index] = "X"
comp_chance()
def comp_chance():
if(len(board) == 0):
print("It's a tie !")
else:
random = choose_random()
board.remove(random)
index = _board.index(random)
del weights[index]
_board[index] = 'O'
user_chance()
user_chance()
我评论了我所做的更改:
import random
from random import choices
board = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] #Changed to strings
weight_values = {"1": 0.1,
"2": 0.05,
"3": 0.1,
"4": 0.05,
"5": 0.3,
"6": 0.05,
"7": 0.1,
"8": 0.05,
"9": 0.1} #Now a dictionary
_board = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9]
def choose_random():
return(choices(board,list(weight_values.values()))) #weight_values.values() returns a list of the weights, which is needed for this function
def draw():
print(_board[0], _board[1], _board[2])
print(_board[3], _board[4], _board[5])
print(_board[6], _board[7], _board[8])
def user_chance():
draw()
choice = (input('Choose any of the block: '))
board.remove(choice) #Remove from board[]
del weight_values[choice] #Remove from weight_values{}
index = _board.index(int(choice))
_board[index] = "X"
comp_chance()
def comp_chance():
if(len(board) == 0):
print("It's a tie !")
else:
random = choose_random()
board.remove(str(random[0])) #Remove from board[]
index = _board.index(int(random[0]))
del weight_values[str(random[0])] #Remove from weight_values{}
_board[index] = 'O'
user_chance()
user_chance()
玩家仍然可以选择已标记的方块,因此需要修复。问题是您在删除板值时没有删除权重值。权重值和板值的数量必须相同。作为字典,管理起来就方便多了。
我在 python 中编写了一个代码来制作一个简单的井字游戏,但错误不断出现。我无法弄清楚问题..请帮忙
您可以在任何编辑器上查看代码 -
import random
from random import choices
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
weights = [0.1, 0.05, 0.1, 0.05, 0.3, 0.05 , 0.1, 0.05, 0.1]
_board = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9]
def choose_random():
return(choices(board,weights))
def draw():
print(_board[0], _board[1], _board[2])
print(_board[3], _board[4], _board[5])
print(_board[6], _board[7], _board[8])
def user_chance():
draw()
choice = int(input('Choose any of the block: '))
board.remove(choice)
index = _board.index(choice)
_board[index] = "X"
comp_chance()
def comp_chance():
if(len(board) == 0):
print("It's a tie !")
else:
random = choose_random()
board.remove(random)
index = _board.index(random)
_board[index] = 'O'
user_chance()
user_chance()
在第 17 行中,当您调用 choose_random() 时,棋盘列表和权重列表的长度不匹配,因此您会收到错误消息。
这里有几个问题需要解决:
您正在从
board
列表而不是weights
列表中删除值,因此您遇到了这个问题choose_random()
是 return 一个k
值的列表,你没有指定k
所以它会 return具有一个值的列表。您应该在 return 语句的末尾添加[0]
。您还应该添加检查用户是否在板上输入了有效位置,否则,您将收到
的错误value is not in list
from random import choices board = [1, 2, 3, 4, 5, 6, 7, 8, 9] weights = [0.1, 0.05, 0.1, 0.05, 0.3, 0.05 , 0.1, 0.05, 0.1] _board = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9] def choose_random(): return(choices(board,weights))[0] def draw(): print(_board[0], _board[1], _board[2]) print(_board[3], _board[4], _board[5]) print(_board[6], _board[7], _board[8]) def user_chance(): draw() choice = int(input('Choose any of the block: ')) board.remove(choice) index = _board.index(choice) del weights[index] _board[index] = "X" comp_chance() def comp_chance(): if(len(board) == 0): print("It's a tie !") else: random = choose_random() board.remove(random) index = _board.index(random) del weights[index] _board[index] = 'O' user_chance() user_chance()
我评论了我所做的更改:
import random
from random import choices
board = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] #Changed to strings
weight_values = {"1": 0.1,
"2": 0.05,
"3": 0.1,
"4": 0.05,
"5": 0.3,
"6": 0.05,
"7": 0.1,
"8": 0.05,
"9": 0.1} #Now a dictionary
_board = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9]
def choose_random():
return(choices(board,list(weight_values.values()))) #weight_values.values() returns a list of the weights, which is needed for this function
def draw():
print(_board[0], _board[1], _board[2])
print(_board[3], _board[4], _board[5])
print(_board[6], _board[7], _board[8])
def user_chance():
draw()
choice = (input('Choose any of the block: '))
board.remove(choice) #Remove from board[]
del weight_values[choice] #Remove from weight_values{}
index = _board.index(int(choice))
_board[index] = "X"
comp_chance()
def comp_chance():
if(len(board) == 0):
print("It's a tie !")
else:
random = choose_random()
board.remove(str(random[0])) #Remove from board[]
index = _board.index(int(random[0]))
del weight_values[str(random[0])] #Remove from weight_values{}
_board[index] = 'O'
user_chance()
user_chance()
玩家仍然可以选择已标记的方块,因此需要修复。问题是您在删除板值时没有删除权重值。权重值和板值的数量必须相同。作为字典,管理起来就方便多了。