Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么会出现值错误:太多的值无法解包?_Python - Fatal编程技术网

Python 为什么会出现值错误:太多的值无法解包?

Python 为什么会出现值错误:太多的值无法解包?,python,Python,我已经开始用Python编写一个程序,在这个程序中,用户输入要向上/向下和向左/向右移动的空格数,并使用了.split()内置函数来执行此操作,但是当我运行该程序时,当只给出两个值时,会出现错误“Value error:太多值无法解包(预期两个值)”。如果有人帮我解决这个问题,我将不胜感激。我已在下面附上我的代码: /编辑-我已在下面附上我的所有代码: choice=0 b=0 oldP=0 newP=0 player_location=' X ' x=8 y=0 xi=0 yi=0 up=0

我已经开始用Python编写一个程序,在这个程序中,用户输入要向上/向下和向左/向右移动的空格数,并使用了.split()内置函数来执行此操作,但是当我运行该程序时,当只给出两个值时,会出现错误“Value error:太多值无法解包(预期两个值)”。如果有人帮我解决这个问题,我将不胜感激。我已在下面附上我的代码: /编辑-我已在下面附上我的所有代码:

choice=0
b=0
oldP=0
newP=0
player_location=' X '
x=8
y=0
xi=0
yi=0
up=0
down=0
left=0
right=0
new_board=[xi][yi]
gold_coins=0
bandits=5
treasure_chests=10
a=1
xi2=0
yi2=0

import random
def menu():
    print('If you would like to play the Treasure Hunt , press 1')
    choice=input('If not, press any key to exit')
    if choice=='1':
        print('Great! You have made the right choice :)')
    else:
        print('Goodbye.')
        quit()
menu()
def grid():
    new_board = [ ]

def board():
  new_board = [ ]
  top_row = [' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ']

  new_board.append(top_row)

  for x in range(0,8):
    row = [' 0 ']*8
    new_board.append(row)
  return new_board

def print_board(b):
  row_numbers = [' ','1','2','3','4','5','6','7','8']
  i = 0
  for row in b:
    print (row_numbers[i],''.join(row))
    i=i+1
new_board = board()
xi=int(8)
yi=int(0)
new_board[xi][yi] = player_location
while a==1:
    def get_move():
        advice  = 'Please enter your move in two integers, vertical, then horizontal.  Use positive numbers for up and right, negative for down and left.'
        example = 'For example, an input of \'22\' would be 2 moves vertically, and 2 moves horizontally.'
        move = input(advice + example)
        move.split()
        x_move,y_move=move
        x_move = int(move[0])
        y_move = int(move[1])
        return x_move, y_move, move

while True:
    print_board(new_board)
    x_move, y_move = get_move() 
    move(x_move, y_move)  # performs the move; updates the board    grid()

完全回溯本来是不错的,但这里的错误很明显:返回3个值
returnx\u move,y\u move,move

然后将返回值分配给两个值:
x\u move,y\u move=get\u move()

删除第三个返回值,因为信息已包含在前2个坐标数据中,因此该值无效

请注意,它之所以失败,是因为python无法决定如何将返回值分配给提供的变量(3=>2)。只给1个变量赋值会起作用,并且会存储3个大小的元组

编辑:Willem answer指出了另一个错误:
move.split()
什么都不做,所以您首先遇到的解包错误就在这里:

x_move,y_move=move

(必须修复这两个错误才能使代码正常工作)

问题可能是
get\u move()
函数返回一个包含三个值的元组,而调用者只想获取两个值:


def get_move():
    advice  = 'Please enter your move in two integers, vertical, then horizontal.  Use positive numbers for up and right, negative for down and left.'
    example = 'For example, an input of \'22\' would be 2 moves vertically, and 2 moves horizontally.'
    move = input(advice + example)
    move.split()
    x_move,y_move=move
    x_move = int(move[0])
    y_move = int(move[1])
    return x_move, y_move, move # three values

    while True:
        print_board(new_board)
        x_move, y_move = get_move() # two values?
        move(x_move, y_move)
此外,请注意,
move.split()
不能内联运行,请记住,
str
(字符串)是不可变的,您需要捕获结果,因此您应该将
get\u move
函数重写为:


def get_move():
    advice  = 'Please enter your move in two integers, vertical, then horizontal.  Use positive numbers for up and right, negative for down and left.'
    example = 'For example, an input of \'22\' would be 2 moves vertically, and 2 moves horizontally.'
    move = input(advice + example)
    move = move.split()
    x_move,y_move=move
    x_move = int(x_move)
    y_move = int(y_move)
    return x_move, y_move, move

def get_move():
advice='请输入两个整数,垂直,然后水平。向上和向右使用正数,向下和向左使用负数。”
示例='例如,输入'22\'将垂直移动两次,水平移动两次。'
移动=输入(建议+示例)
move=move.split()
x_移动,y_移动=移动
x_move=int(x_move)
y_move=int(y_move)
返回x_移动,y_移动,移动

或者类似的东西。

请向我们展示您对系统的输入。回溯或您得到的不准确/猜测答案(最终提供了解决方案,但付出了更大、不必要的努力)刚刚用我的其余代码更新了我的帖子!谢谢:)代码的其余部分是不必要的。我们所需要的只是对异常的追溯。好吧,现在已经解决了。事实上,拆分的事情让我相信解包错误首先来自
x_-move,y_-move=move
。@Jean-Françoisfare:似乎有两个序列解包命令至少容易出错。此外,第一个(在方法中)有点危险,因为它在赋值完成之前没有经过测试。:)@让·弗朗索瓦·法布、威廉·范昂森感谢您的反馈!我知道我应该首先包含一个完整的回溯,但我返回“move”的原因是为了以后可以在代码中执行移动,现在可以看到。不过,我现在明白了,我需要一种不同的方法来执行移动:)@user5095215您将随着时间的推移而改进:)

def get_move():
    advice  = 'Please enter your move in two integers, vertical, then horizontal.  Use positive numbers for up and right, negative for down and left.'
    example = 'For example, an input of \'22\' would be 2 moves vertically, and 2 moves horizontally.'
    move = input(advice + example)
    move = move.split()
    x_move,y_move=move
    x_move = int(x_move)
    y_move = int(y_move)
    return x_move, y_move, move