我在python中有一个for循环,它跳过了字符串中的最后一个字符,我试着像这样打印变量:
for i in "125": print (i)
它没有打印5,这是我的脚本:
OctalBinary1 = {
"0" : "000",
"1" : "001",
"2" : "010",
"3" : "011",
"4" : "100",
"5" : "101",
"6" : "110",
"7" : "111"
}
def toBinaryOctal(x):
counter = 0
sum_var = ""
fin_var = ""
for i in x:
counter += 1
sum_var += str(i)
if (counter != 3):
for x , y in OctalBinary1.items():
if x == sum_var:
fin_var += OctalBinary1[x]
sum_var = ""
else:
print ("Did not find a match")
print (i)
return fin_var
print (toBinaryOctal("125"))
答案 0 :(得分:1)
for i in x:
counter += 1
sum_var += str(i)
if (counter != 3):
当for循环到达"5"
时,计数器将在行if (counter != 3)
处为3,因此它将在之后立即中断循环。这就是为什么你没有得到"5"
输出。