我正试图制作一个战舰游戏,但我被困在船的一代。 当我试着运行这个程序时:
print "Welcome to Battleships!!!"
board = []
import os
from random import randint
for x in range(10):
board.append(["O"] * 10)
def print_board(board):
for row in board:
print " | ".join(row)
ships = {"battleship" : [5], "cruiser1" : [4], "cruiser2" : [4], "frigate1" : [3], "frigate2" : [3], "frigate3" : [3], "frigate4" : [3], "minesweeper1" : [2], "minesweeper2" : [2], "minesweeper3" : [2], "minesweeper4" : [2]}
numbers1 = []
numbers2 = []
numbers = []
setup_nums1()
setup_nums2()
setup_ships()
print numbers1
print numbers2
print ships
我得到这个错误:
Welcome to Battleships!!!
Traceback (most recent call last):
File "/home/matthew/Documents/battleship2.py", line 77, in <module>
setup_ships()
File "/home/matthew/Documents/battleship2.py", line 41, in setup_ships
for ship, info in ships:
ValueError: too many values to unpack
要解包的值太多意味着调用的函数返回多个值,而您只处理其中的一些值。在
问题出在
这一定是
^{pr2}$或
而不是
for ship, info in ships
尝试for ship, info in ships.iteritems()
。在使用
in ships
语法,您只能返回一个值(它们的键)。在例如,您可以:
相反,如果你这样做:
^{pr2}$有关python2.x和python3.x兼容性的更多信息,请查看this answer。在
相关问题 更多 >
编程相关推荐