2017-01-22 59 views
-1

我已經開始在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

請向我們展示您給系統的輸入。 –

+0

追溯或你得到不準確/猜測的答案(最終提供的解決方案,但更大,不必要的努力) –

+0

剛剛更新我的帖子與我的其他代碼!謝謝:) – user5095215

回答

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(串)是不可改變的,你需要捕捉的結果,所以你應該重寫你的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 =move.split() 
    x_move,y_move=move 
    x_move = int(x_move) 
    y_move = int(y_move) 
    return x_move, y_move, move 

或等價的東西。

+0

實際上,分裂的事情讓我相信解壓錯誤首先來自'x_move,y_move = move'。 –

+0

@ Jean-FrançoisFabre:好吧,似乎有兩個序列解包命令至少容易出錯。此外,第一個(在該方法中)有點危險,因爲它在分配完成之前未經過測試。 :) –

+0

別擔心,問題不是很好。 –

1

完整回溯本來不錯,但這裏的錯誤是顯而易見的:你回3個值return x_move, y_move, move

和分配返回值2倍的值:​​

刪除第三返回值,因爲無用信息已經包含在第2個座標數據

注意它只會失敗,因爲python無法決定如何將返回值分配給提供的變量(3 =>​​ 2)。分配給只有1個變量會起作用,並且會存儲3個大小的元組。

編輯:威廉回答指出,另一個錯誤:move.split()什麼也不做,讓你先encouter的解包誤差是存在的:

x_move,y_move=move 

(你必須解決這兩個錯誤,爲您的代碼工作)

+0

啊,我明白了,謝謝! – user5095215

相關問題