我需要扫描几个字符串的列表(每行是字符串),找到符号“+”或“@”(只能出现其中一个),然后打印x,y坐标。左角是(0,0),如果你保持正确和向下,则值会增加。
我不明白我的代码有什么问题:
x,y=-1,-1
for row in my_list:
y+=1
for symbol in row:
x+=1
if symbol=="@":
makom=(x,y)
player="@"
if symbol=="+":
makom=(x,y)
player="+"
print makom
列表:
####
# .#
# ###
#*@ #
# $ #
# ###
####
输出:
(2,3)
答案 0 :(得分:2)
您在更改行时忘记重置x
:
y = -1
for row in my_list:
x = -1
y += 1
for symbol in row:
x += 1
if symbol == "@":
makom = (x,y)
player = "@"
if symbol == "+":
makom = (x,y)
player = "+"
print makom
为了完整起见,如果您使用enumerate
:D
for y, row in enumerate(my_list):
for x, symbol in enumerate(row):
if symbol == "@":
makom = (x, y)
player = "@"
if symbol == "+":
makom = (x, y)
player = "+"
print makom