Reputation: 27
I have a for loop in python and it skipped the last character in the string, I tried printing the variable like so:
for i in "125": print (i)
and it didnt print the 5, here's my script:
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"))
Upvotes: 0
Views: 71
Reputation: 4866
for i in x:
counter += 1
sum_var += str(i)
if (counter != 3):
When the for loop reaches "5"
counter will be 3 at line if (counter != 3)
, so it will break the loop right after. That's why you don't get "5"
output.
Upvotes: 1