Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[Python] Wat doe ik fout met while loop?

Pagina: 1
Acties:

  • HollowGamer
  • Registratie: Februari 2009
  • Niet online
Beste Tweakers,

Voor school ben ik bezig met een (Python3) zeeslag opdracht.

Ik ben bijna klaar, alleen een ding lukt gewoon niet:
Er draait een While loop die checkt of een schip gezet kan worden of niet, door te checken of een blokje al is ingevuld door een ander schip. Als dit het geval is: return False, en probeer opnieuw.
Nu lukt dit wel, en wordt er dus het betreffende schip opnieuw geprobeerd te zetten.

Setship functie:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# function to random set ship
def setShip(ship):

...

valid = False
    for block in blocks:
        default = False
        blockHorFilled = setPositions.get(str(row) + str(block), default)
        blockVerFilled = setPositions.get(str(block) + str(row), default)           
        if not blockHorFilled or not blockVerFilled:
            valid = True # block already filled, return False

    if valid:
        ship['uuid'] = str(uuid.uuid4()) # unique Id
        for block in blocks:
            setPositions[str(row) + str(block)] = ship
        return True # ship set successfully
    else:
        return False


while-loop:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
# begin placing ships by levelShips
count = 0
while True:
    if count == levelShips: # max. ships released
        break
    
    randomShip = random.choice(validShips) # choose between the ships
    shipSet = setShip(ships[randomShip])
    if (shipSet):
        print(count)
        count = count + 1
    else:
        print('not: ' + str(count))


Resultaat:
code:
1
{'4g': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, 'j7': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '9d': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': '1155bfc0-2682-47f1-bfce-8cb3261280ee'}, '9c': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': '1155bfc0-2682-47f1-bfce-8cb3261280ee'}, 'j9': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '4e': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '9h': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, 'h8': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '6c': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '2f': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '6e': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '2d': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '9g': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '9f': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '2b': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '4f': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, 'j8': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, 'h6': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '4i': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '2c': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '3i': {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 'f4a9d994-607e-4dde-9490-d79aa097d573'}, 'h7': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '9e': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': '1155bfc0-2682-47f1-bfce-8cb3261280ee'}, 'h5': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '9i': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, 'j6': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': '865f4442-01a1-42de-bccc-749f4dcabad0'}, '6d': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '6f': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '2e': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '3h': {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 'f4a9d994-607e-4dde-9490-d79aa097d573'}, '6b': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}, '4h': {'Blocks': 5, 'Name': 'Slagschip', 'uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e'}}


Een uuid (''uuid': 'f7f0a7aa-ceb5-43ec-839c-dc6417152d2e') komt bijvoorbeeld 15x voor, wat totaal niet kan. :S

Ik snap er helemaal niks meer van, is er nu een fout in mijn while-loop, setShip functie? Stackoverflow's geven mij helaas geen antwoord, hopelijk kan iemand mij verder helpen. :)

Voor de volledige source: http://pastebin.com/SmXfFita

Heel erg bedankt!

  • RayNbow
  • Registratie: Maart 2003
  • Laatst online: 10:48

RayNbow

Kirika <3

Algemene tip: Gebruik pprint om je resultaat leesbaarder op 't scherm te krijgen.

Ipsa Scientia Potestas Est
NNID: ShinNoNoir


  • HollowGamer
  • Registratie: Februari 2009
  • Niet online
RayNbow schreef op zondag 25 mei 2014 @ 15:22:
Algemene tip: Gebruik pprint om je resultaat leesbaarder op 't scherm te krijgen.
Thanks! Zal dit de volgende keer proberen.

Nog wat debug info:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Block: 2d = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 0} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 0}
Block: 2e = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 0} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 0}
Block: 2f = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 0} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 0}
Block: 8c = {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 1} same as {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 1}
Block: 8d = {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 1} same as {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 1}
Block: 4e = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2}
Block: 4f = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2}
Block: 4g = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2}
Block: 4h = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 2}
Block: 6d = {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3} same as {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3}
Block: 6e = {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3} same as {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3}
Block: d3 = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 4} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 4}
Block: d4 = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 4} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 4}
Block: d5 = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 4} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 4}
Block: 10h = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5}
Block: 10i = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5}
Block: 10j = {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5} same as {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5}
Block: j3 = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6}
Block: j4 = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6}
Block: j5 = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6}
Block: j6 = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 6}
Block: 10d = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7}
Block: 10e = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7}
Block: 10f = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7}
Block: 10g = {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7} same as {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7}


SetPositions (zou zelfde resultaat als hierboven moeten returnen > uuid):
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{   '10d': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '10e': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '10f': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '10g': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '10h': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    '10i': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    '10j': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    '2d': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    '2e': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    '2f': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    '4e': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '4f': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '4g': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '4h': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    '6d': {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3},
    '6e': {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3},
    '8c': {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3},
    '8d': {'Blocks': 2, 'Name': 'Mijnenveger', 'uuid': 3},
    'd3': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    'd4': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    'd5': {'Blocks': 3, 'Name': 'Fregatten', 'uuid': 5},
    'j3': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    'j4': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    'j5': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7},
    'j6': {'Blocks': 4, 'Name': 'Kruiser', 'uuid': 7}}

[ Voor 55% gewijzigd door HollowGamer op 25-05-2014 15:28 ]


  • HollowGamer
  • Registratie: Februari 2009
  • Niet online
Inmiddels is het probleem verholpen, dankzij een mede klasgenoot :)
Het schijnt zo te zijn dat Python 'objecten' (hard-)linkt, zelfs in functies.
In PHP is dit niet het geval, maar in Python dus wel.

Het resultaat was, dat een aangeroepte dictonary werd gevuld, die alleen als referentie zou moeten dienen.

Oplossing:
code:
1
2
3
4
5
6
import copy

ship = {'Name': 'Slagschip', 'Blocks': 5}

shipData = copy.deepcopy(ship) # needed, otherwise linked-object
shipData['uuid'] = Id # unique Id