我試圖打印lst
列表中的每個單個字符的拼寫字母表,併爲列表中的其他任何內容打印0。我的代碼如下:蟒蛇,因爲不工作
def alphabet(lst):
returned = []
for value in lst:
value = str(value)
if value == "a":
returned = returned + ["alpha"]
elif value == "b":
returned = returned + ["bravo"]
elif value == "c":
returned = returned + ["charlie"]
#
# lots of elifs removed here for brevity
#
elif value == "y":
returned = returned + ["yankee"]
elif value == "z":
returned = returned + ["zulu"]
else:
returned = returned + ["0"]
return returned
通過此功能給出的輸出總是["0"]
每個輸入。爲什麼for
構造不適用於列表上的每個項目?我怎樣才能將return
語句從for
循環中刪除而不會發生縮進錯誤?
調試你的代碼。 – Maroun 2014-10-08 09:19:17
向我們展示'lst'的樣本值。 – 2014-10-08 09:19:23
我覺得你說錯了,因爲把它稱爲'字母'('something')'適用於我。也許你把它叫做'字母表'(['something'])'? – parchment 2014-10-08 09:24:34