[Tutor] KeyError
Dragonfirebane at aol.com
Dragonfirebane at aol.com
Mon Jun 14 16:59:58 EDT 2004
Hello all,
Using the command prompt to run my program, the following error occurs. I do
not believe it is because I'm using the command prompt because the same error
occurs in IDLE. Since I used the same structure to convert text to binary as
I did to convert text to hexadecimal (see code below error), I do not
understand why the hexadecimal conversion doesn't work but the binary does.
Additionally, much of the commenting had not been updated and is no longer correct, so
please disregard any apparent misunderstanding through comments.
C:\Program Files\Python 2.3.4c1\Programming\Programs (Complete)>python
hexadecimal-binary-text.py
Please enter numbers or text to be converted. "Hello. My name is Bob," said
Fred.
Convert to:
1: Binary
2: Hexadecimal
3: Decimal
2
Traceback (most recent call last):
File "Hexadecimal-Binary-Text.py", line 128, in ?
convertxthex() ##convert text to hexadecimal
File "Hexadecimal-Binary-Text.py", line 36, in convertxthex
res += hexadec[c] ##same deal
KeyError: 'C'
def convertxtbin(): ##convert text to binary
for char in original: ##loops over the entire entry
x = 0 ##allows checking every character against the entire alphabet
list
if char in punct:
res += char
elif char in alphabet:
while x <= 26: ##if the character is punctuation or a lowercase
letter
if char in alphabet[x]: ##if its in the dictionary
asOctal = "%o" % int(number[x])
global res
for c in asOctal:
res += binary[c] ##add the corresponding entry in
the binary list to the res list
if char not in punct: ##if the character isn't punctuation
res += ' ' ##add a space at the end to separate
numbers
x += 1 ##adds 1 to the value of x so the loop doesnt go on
forever
while 26 < x <= 52: ##if the character is an uppercase letter
if char in alphabet[x]: ##if the character is in the alphabet
dictionary
asOctal = "%o" % int(number[x - 26])
global res
for c in asOctal:
res += binary[c] ##print the corresponding entry in
the binary entry (A = entry 36, so 36 - 26 = entry 10 = '01' = a)
if char not in punct: ##if character isn't punctuation
res += ' ' ##add a space at the end
x += 1 ##adds 1 to x so the loop ends eventually
def convertxthex(): ##convert text to hexadecimal
for char in original: ##same deal
x = 0 ##same deal
if char in punct:
res += char
elif char in alphabet:
while x <= 26: ##same deal
if char in alphabet[x]: ##same deal
asHex = "%X" % int(number[x])
global res
for c in asHex:
res += hexadec[c] ##same deal Line 36
if char not in punct: ##same deal
res += ' ' ##same deal
x += 1 ##same deal
while 26 < x <= 52: ##same deal
if char in alphabet[x]: ##same deal
asHex = "%X" % int(number[x - 26])
global res
for c in asHex:
res += hexadec[c] ##same deal
if char not in punct: ##same deal
res += ' ' ##same deal
x += 1 ##same deal
def convertxtnum(): ##convert text to decimal
for char in original: ##same deal
x = 0 ##same deal
while x <= 34: ##same deal
if char in alphabet[x]: ##same deal
res.append(number[x]) ##same deal
if char not in alphabet[:8]: ##same deal
res.append(' ') ##same deal
x += 1 ##same deal
while 34 < x <= 60: ##same deal
if char in alphabet[x]: ##same deal
res.append(number[x - 26]) ##same deal
if char not in alphabet[:8]: ##same deal
res.append(' ') ##same deal
x += 1 ##same deal
def convertnum(): ##converts numbers
if whichconv in('1','Binary','binary','Bin','bin','B','b'): ##if user
wants to convert to binary
if int(original) <= 0:
print "Please enter a positive number. "
try:
int(original)
except ValueError:
x = 0
if char in punct[x]:
print [punct[x] for c in original] ##prints the corresponding
binary entry (if '12' is entered, it is treated as a string, so
binary[int(original) - 1] converts it to an integer and subtracts one from it so that the
corresponding entry can be printed since the first entry in a list is 0
x += 1
else:
asOctal = "%o" % int(original)
print ''.join([binary[c] for c in asOctal])
elif whichconv
in('2','Hexadecimal','hexadecimal','Hexadec','hexadec','Hex','hex','H','h'): ##same deal
if original >= 0:
print hexadec[int(original) + 8] ##same deal
else:
print "Please enter a positive number. "
elif whichconv in('3','Text','text','T','t'): ##same deal
if int(original) <= 26: ##if the number is between 8 and 27 (the
lowercase letters of the alphabet list
print alphabet[int(original) + 8] ##it prints the corresponding
entry in the alphabet list
else: ##if its not one of those numbers
print "Sorry, you didn't enter a valid number. Please enter only
numbers between 0 and 26." ##prints message
import time ##for time.sleep(1.1) at end
import string
alphabet = ['
','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I',
'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
##define the alphabet list
punct = ['.',',',';',':','?','!','"',"'"]
binary = {'0':'000','1':'001','2':'010',
'3':'011','4':'100','5':'101',
'6':'110','7':'111'} ##define the binary dictionary
number =
['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','
31','32','33','34'] ##defines the number entry. goes up to 34 so
convertnum() will work under the text conversion
hexadec =
{'0':'0','1':'1','2':'2','3':'3','4':'4','5':'5','6':'6','7':'7','8':'8','9':'9','10':'A','11':'B','12':'C','13':'D','14':'E','15':'F'}
##defines the hexadecimal list
res = '' ##makes an empty list so all the conversions will work
again = True ##beginning condition for loop
while again: ##starts loop
original = raw_input("Please enter numbers or text to be converted. ")
##enter text to be converted
try:
int(original) ##distinguishes the numbers from the text, needs to
be revised to distinguish binary and hexadecimal
## except TypeError:
## whichconv = raw_input("""Convert to:
##1: Binary
##2: Hexadecimal
##3: Decimal
##""")
## if whichconv in('1','Binary','binary','Bin','bin','B','b'):
## convertxtbin()
## print ''.join(res)
## elif whichconv
in('2','Hexadecimal','hexadecimal','Hexadec','hexadec','Hex','hex','H','h'):
## convertxthex()
## elif whichconv in('3','Decimal','decimal','Dec','dec','D','d'):
## convertxtnum()
except ValueError: ##if its not numbers
whichconv = raw_input("""Convert to:
1: Binary
2: Hexadecimal
3: Decimal
""") ##what conversion?
if whichconv in('1','Binary','binary','Bin','bin','B','b'): ##if
binary
convertxtbin() ##convert text to binary
elif whichconv
in('2','Hexadecimal','hexadecimal','Hexadec','hexadec','Hex','hex','H','h'): ##if hexadecimal
convertxthex() ##convert text to hexadecimal Line 128
elif whichconv in('3','Decimal','decimal','Dec','dec','D','d'): ##if
decimal
convertxtnum() ##convert text to decimal
print res ##print the list entries together on a line
else: ##if the text is numbers
whichconv = raw_input("""Convert to:
1: Binary
2: Hexadecimal
3: Text
""") ##which conversion?
str(original) ##convert numbers back to a string
convertnum() ##convert numbers to whatever
more = raw_input("Would you like to convert more text or numbers [y/n]?
") ##more?
if more in 'Yy': ##if so, continue loop
pass
elif more in 'Nn': ##if not,
again = False ##break loop
print "Thank you for using Multivert. Multivert will now close" ##loop
broken, prints message
time.sleep(1.1) ##waits for 1.1 seconds so message can be read
##end of program; program closes
Thanks in advance,
Orri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040614/efd1d0b8/attachment-0001.html
More information about the Tutor
mailing list