0
我覺得我做錯了什麼在for循環和用戶輸入如果檢查循環變量是字符串(不變量==字符串[index_Integer])
的最後一個索引每當我爲循環做,我相信,在for循環中的局部變量在列表中
list[index_int] #returns value of the indexed entry in the list
user_input = "1234"
user_input[-1] >>> 4
def print_numbers(n):
for entry in n:
if entry == "1":
if (entry == n[-1]):
print "E"
else:
print entry + "s,",
elif entry == "0":
if (entry == n[-1]):
print "X"
else:
print entry + "X,",
else:
if (entry == n[-1]): #applies if entry is equal to the value of user input at index value of (-1), which I do not want
print entry + "L"
else:
print entry + "s,",
user_input = raw_input()
if user_input.isdigit():
print_numbers(user_input)
else:
print user_input
只有一個入口當我想放下101或212,我得到這些
Intention -> 1s, X, E
Result:
E
X, E
Intention -> 2s, 1s, 2L
Result:
2L
1s, 2L
我相信,我的原因在for循環中,每個變量在其自己的列表中只有一個元素。
是否沒有辦法檢查for循環是否在AT列表(字符串)的最後一個索引位置(NOT的變量等於字符串索引的值,即entry == n [-1])?
我明白你的意思了:) – 2012-04-22 04:13:17
謝謝!我已經瞭解到我可以使用列表split:list [i:j] 並分配這樣的新字符串來創建單獨的字符串: ones = n [len(n)-1:]和other_digits = n [: len(n)-1] ,我把他們每個人在各自的功能迴路 感謝您的幫助,雖然 – JBRPG 2012-04-23 02:53:40