Your approach is not quite right here.
For one, at the end of your while loop you don't set num3 back to "3", so your for loop gets "off" since it depends on this, so you get a different value the second time through the loop, even though binary has not changed.
For two, the second time through the while, binary is the same as the first time through... you'll get the same value for letter (which will be the first 8 characters of binary) both times through.
Third, The means of building letter is pretty strange/involved. I don't know what the book as covered so far, but if you wanted to just accept a single character at a time, you could just do this (no error checking in this example):
Code:
binToChar = {"01000001" : "a",
"01000010" : "b",
"01000011" : "c",
"01000100" : "d",
"01000101" : "e",
"01000110" : "f",
"01000111" : "g",
"01001000" : "h",
"01001001" : "i",
"01001010" : "j",
"01001011" : "k"}
binary = raw_input("Enter 8 numbers to find the letter: ")
print "The letter is: " + binToChar[binary]
If you want to accept more than one character in binary at a time, you'll need to figure out how many characters are present, and grab 8 characters at a time and convert them. Again, I don't know how far you are (i.e. if sequence slicing has been covered, the in statement, etc.), but that would look something like this:
Code:
binToChar = {"01000001" : "a",
"01000010" : "b",
"01000011" : "c",
"01000100" : "d",
"01000101" : "e",
"01000110" : "f",
"01000111" : "g",
"01001000" : "h",
"01001001" : "i",
"01001010" : "j",
"01001011" : "k"}
binary = raw_input("Enter bits in sets of 8 to get a series of characters: ")
if len(binary)%8 != 0:
print "Invalid number of bits!"
else:
startpos=0
while startpos < len(binary):
singleLetter = binary[startpos:startpos+8]
if singleLetter in binToChar.keys():
print binToChar[singleLetter]
else:
print singleLetter + " is not a valid binary representation of an ASCII character"
startpos+=8
Instead of using "in" in an if, it could be in a for if you wanted to do the search that way. As for the sequence slicing... I'm not sure how I'd approach that otherwise. I suppose you could use your num1-num8 idea, and loop over the characters in binary, keeping a count, then print each 8, and start again. Maybe something like:
Code:
binToChar = {"01000001" : "a",
"01000010" : "b",
"01000011" : "c",
"01000100" : "d",
"01000101" : "e",
"01000110" : "f",
"01000111" : "g",
"01001000" : "h",
"01001001" : "i",
"01001010" : "j",
"01001011" : "k"}
binary = raw_input("Enter bits in sets of 8 to get a series of characters: ")
if len(binary)%8 != 0:
print "Invalid number of bits!"
else:
curPos = 0
for singleChar in binary:
whichPos = curPos % 8
curPos+=1
if(whichPos == 0):
num0=singleChar
elif (whichPos == 1):
num1=singleChar
elif (whichPos == 2):
num2=singleChar
elif (whichPos == 3):
num3=singleChar
elif (whichPos == 4):
num4=singleChar
elif (whichPos == 5):
num5=singleChar
elif (whichPos == 6):
num6=singleChar
elif (whichPos == 7):
num7=singleChar
singleLetter = num0 + num1 + num2 + num3 + num4 + num5 + num6 + num7
found=False
for key in binToChar.keys():
if singleLetter == key:
found=True
if found:
print binToChar[singleLetter]
else:
print singleLetter + " is not a valid binary representation of an ASCII character"
This doesn't use anything you didn't use except % and .keys(). I wouldn't normally want to do something like that, but I didn't know if you had covered generating your own arrays, etc.
One of the problems with "working through" learning something and trying to implement example code is that you are forced to do things in an unnatural way. It's important to learn how the basics work first, but in practice some things would never be done because there are much better constructs to handle them.
-Lee