【发布时间】:2018-06-02 13:12:16
【问题描述】:
从随机导入randint
def apple(player):
"""Returns a new unoccupied position (tuple). Used to spawn a new `apple` at
that position. Microbit LED 5x5"""
Args:
player (list of tuples): List of player coordinates
Returns:
A new unoccupied position (tuple)
"""
apple = list((0,0))
# loop to get each tuple, as list
for i in range(0, len(player)):
p = list(player[i])
dot[0] = randint(0,4)
dot[1] = randint(0,4)
x= list (apple)
if x!= p:
return tuple((dot))
如果我用
运行函数player = [(0,0), (1,0), (2,0), (3,0), (4,0),
(0,1), (1,1), (2,1), (3,1), (4,1),
(0,2), (1,2), (2,2), (3,2), (4,2),
(0,3), (1,3), (2,3), (3,3), (4,3),
(0,4), (1,4), (2,4), (3,4),]
它应该给我 (4,4),但它确实有效。 整个函数忽略了我的不平等陈述:(
【问题讨论】:
-
len(player)=24 我想你可能会注意到这里并纠正自己
-
如果你想做的是在地图上找到一个空闲位置,为什么还要使用 randint?
标签: python-3.x list if-statement random tuples