如果检查循环变量是字符串(不变量==字符串[index_Integer])
问题描述:
我觉得我做错了什么在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