1

I'm trying to make a Battleship game but I'm stuck at the ship generation. When I'm trying to run this:

Initialization

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 = []

Some Setup Functions

def setup_nums1():
    for count1 in range(6):
        for count2 in range(10):
            number1 = (count1)*100
            number2 = count2
            numbers1.append(number1 + number2)
def setup_nums2():
    for count1 in range(10):
        for count2 in range(6):
            number1 = (count1)*100
            number2 = count2
            numbers2.append(number1 + number2)
def setup_nums():
    for count1 in range(10):
        for count2 in range(10):
            number1 = (count1)*100
            number2 = count2
            numbers.append(number1 + number2)
def setup_numbers():
    setup_nums()
    setup_nums1()
    setup_nums2()
def setup_ships(): 
    x = 59 
    for ship, info in ships:
        direction = randint(1,2)
        if direction == 1:
            ships[ship].append(numbers1[randint (0,x)])
            y = info[1]
            print y
            numbers1.remove(y)
            if y in numbers2:
                numbers2.remove(y)
            numbers.remove(y)
            x = x - 1
            for n in range(ship[0]):
                ships[ship].append(y + n  + 1)
                numbers1.remove(y + n + 1)
                if (y + n + 1) in numbers2:
                    numbers2.remove(y + n + 1)
                numbers.remove(y + n + 1)
                x = x - 1                
        if direction == 2:
            ships[ship].append(numbers2[randint (0,x)])
            y = info[1]
            print y
            numbers2.remove(y)
            if y in numbers1:
                numbers1.remove(y)
            numbers.remove(y)
            x = x - 1
            for n in range(ship[0]):
                ships[ship].append(y + n*100  + 100)
                numbers2.remove(y + n*100 + 100)
                if (y + n + 1) in numbers1:
                    numbers1.remove(y + n*100 + 100)
                numbers.remove(y + n*100 + 100)  
                x = x - 1

Main program

setup_nums1()
setup_nums2()
setup_ships()
print numbers1
print numbers2
print ships

I get this error:

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
Community
  • 1
  • 1

2 Answers2

3

Too many values to unpack means that the function you are calling returns several values and you are only taking care of some of these values.

problem is in

for ship, info in ships:

this must be like

for ship, info in ships.items():

OR

for ship, info in ships.iteritems():
Pablo Jomer
  • 9,870
  • 11
  • 54
  • 102
Nilesh
  • 20,521
  • 16
  • 92
  • 148
3

Instead of for ship, info in ships try for ship, info in ships.iteritems().

Using the in ships syntax, you are only able to return one value (they key).

For example, you could do:

ships = { 'a': 'ship_a', 'b': 'ship_b', 'c': 'ship_c' }
for key in ships:
    print(key) # Outputs a b c

Instead, if you do:

for key, value in ships.iteritems():
    print(key, value) # Outputs ('a', 'ship_a') ('b', 'ship_b') ('c': 'ship_c')

Check this answer for more information about Python 2.x and Python 3.x compatibility.

Community
  • 1
  • 1
Aylen
  • 3,524
  • 26
  • 36