我已经开始在Python中编写一个程序,用户可以在其中输入他们想要向上/向下和向左/向右移动的空间数,并使用.split()内置函数来执行此操作,但是当我运行时程序我得到错误'值错误:太多值解包(两个预期)'只给出两个。如果有人帮我解决了这个问题,我将非常感激。我在下面附上了我的代码: /编辑 - 我在下面附上了我的所有代码:
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()
答案 0 :(得分:1)
完整的回溯本来不错,但错误很明显:你要返回3个值return x_move, y_move, move
并将返回值分配给2个值:x_move, y_move = get_move()
删除第3个返回值,因为信息已经包含在前2个坐标数据
中,所以没用请注意,它只会失败,因为python无法决定如何将返回值分配给提供的变量(3 => 2)。仅分配给1个变量就可以了,并且会存储3个大小的元组。
编辑:Willem回答指出了另一个错误:move.split()
什么也没做,所以你先打包的解包错误就在那里:
x_move,y_move=move
(您必须修复两个错误才能使代码正常工作)
答案 1 :(得分:1)
问题可能是你的get_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)
如果您不关心这些值,您可以丢弃剩余的,例如使用x_move, y_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()
move(x_move, y_move)
另外请注意move.split()
不能内联,请记住str
(字符串)是不可变的,您需要捕获结果,因此您应该重写{{ 1}}功能:
get_move
或类似的东西。