简体   繁体   English

while循环不正常(Python)

[英]while loop not working correctly (Python)

I have come across a problem with this code and I was wondering if anyone knew how to fix it. 我遇到了这个代码的问题,我想知道是否有人知道如何解决它。

This code is for a game based on a board and this code creates two lists of 20 coordinates under each of the variables treasure_coords and bandit_coords . 此代码适用于基于棋盘的游戏,此代码在每个变量treasure_coordsbandit_coords下创建两个20坐标列表。 These lists of coordinates can not have the coordinate (0, 11) in them and none of the co-ordinates can be in both lists. 这些坐标列表中不能包含坐标(0, 11) ,并且两个坐标中都不能有坐标。

I thought I had stopped this happening by adding bandit_coords in treasure_coords to my while loop but when the code runs it still creates some of the same coordinates in both lists, and this is the problem. 我以为我已经通过bandit_coords in treasure_coords我的while循环bandit_coords in treasure_coords添加到我的while循环来阻止了这种情况,但是当代码运行时它仍会在两个列表中创建一些相同的坐标,这就是问题所在。 Please reply with any useful feedback. 请回复任何有用的反馈。

import random
board = []
for x in range(12):
    board.append(["[ ]"]*12)
coord_creater = [(x, y) for x in range(len(board[0])) for y in range(len(board))]
treasure_coords = random.sample(coord_creater, 20)
bandit_coords = random.sample(coord_creater, 20)
while (0,11) in treasure_coords or (0,11) in bandit_coords or bandit_coords in treasure_coords:
    treasure_coords = random.sample(coord_creater, 20)
    bandit_coords = random.sample(coord_creater, 20)
print (treasure_coords)
print (bandit_coords)

You cannot use in to check if any element of a list is inside another. 您不能使用in来检查列表中的任何元素是否在另一个元素中。 in checks if an individual element is inside a list. in检查的单个元素是一个列表内。

In this case, any is your friend. 在这种情况下, any都是你的朋友。 This will check if any of the elements in a list is True. 这将检查列表中的任何元素是否为True。 Combining this with generators will do the trick. 将其与发电机相结合将起到作用。

bandit_coords in treasure_coords

Needs to be replaced by: 需要替换为:

any([coord in treasure_coords for coord in bandit_coords ])

This will create a list with a Boolean checking if every single coordinate in bandit_coords is in treasure_coords and then check if any of them is True 这将创建一个带有布尔值的列表,检查bandit_coords中的每个坐标是否都在treasure_coords中,然后检查它们中的任何一个是否为True

OK, I'm not sure that a while loop is the best way to go, but here are two possible implementations: 好的,我不确定while循环是最好的方法,但这里有两个可能的实现:

import random
board = []
for x in range(12):
    board.append(["[ ]"]*12)
possible_cords = [(x,y) for x in range(len(board[0])) for y in range(len(board))]

# we don't want any item at (0,11)
invalid_cord = possible_cords.pop(11)
treasure_cords = list()
bandit_cords = list()

Option 1: 选项1:

for x in range(20):
    treasure_at = random.choice(possible_cords)
    possible_cords.pop(possible_cords.index(treasure_at))
    bandit_at = random.choice(possible_cords)
    possible_cords.pop(possible_cords.index(bandit_at))
    treasure_cords.append(treasure_at)
    bandit_cords.append(bandit_at)
print zip(treasure_cords, bandit_cords)

Option 2: 选项2:

# or if you must use a while loop
count = 0
while count < 20:
    treasure_at = random.choice(possible_cords)
    possible_cords.pop(possible_cords.index(treasure_at))
    bandit_at = random.choice(possible_cords)
    possible_cords.pop(possible_cords.index(bandit_at))
    treasure_cords.append(treasure_at)
    bandit_cords.append(bandit_at)
    count += 1
print zip(treasure_cords, bandit_cords)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM