# [Tutor] KeyError

Dragonfirebane at aol.com Dragonfirebane at aol.com
Mon Jun 14 22:44:19 EDT 2004

```Skipped content of type multipart/alternative-------------- next part --------------
def convertxtbin():                                             ##convert text to binary
for char in original:                                       ##loops over the entire entry
global res
x = 0                                                   ##allows checking every character against the entire alphabet list
if char in punct:                                       ##if character is punctuation
res = split(res)                                    ##prints contents of res into a list and names it res
res.append(' ')                                     ##adds a space at the end
del res[-1]                                         ##deletes the space
res = ' '.join(res)                                 ##returns res to string form
elif char in alphabet:                                  ##if the character is in the alphabet
while x <= 26:                                      ##if the character is  a lowercase letter or space
if char in alphabet[x]:                         ##redundant. only there to allow x += 1 to stay inside the while loop but outside the ifs
asOctal = "%o" % int(number[x])             ##converts alphabet number (a = 1) to an unsigned octal
for char in asOctal:                        ##for character in resulting octal
res += binary[char]                     ##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])        ##converts alphabet number (A = 1) to an unsigned octal
for char in asOctal:
res += binary[char]
if char not in punct:
res += ' '
x += 1
def convertxthex():                                             ##convert text to hexadecimal
for char in original:
global res
x = 0
if char in punct:
res = split(res)
res.append(' ')
del res[-1]
res = ' '.join(res)
res += char
elif char in alphabet:
while x <= 26:
if char in alphabet[x]:
asHex = "%X" % int(number[x])               ##convert alphabet number to corresponding Hexadecimal number
if char not in punct:
res += ' '
x += 1
while 26 < x <= 52:
if char in alphabet[x]:
asHex = "%X" % int(number[x - 26])          ##convert alphabet number to corresponding hex number
res += asHex
if char not in punct:
res += ' '
x += 1
def convertxtnum():                                             ##convert text to decimal
for char in original:
global res
x = 0
if char in punct:
res = split(res)
res.append(' ')
del res[-1]
res = ' '.join(res)
res += char
elif char in alphabet:
while x <= 52:
if x == 0:
if char == alphabet[x]:                     ##if character is a space
res += '%d' % int(ord(char) - 32)       ##add a space to the string
if char not in punct:
res += ' '
x += 1
elif 0 < x <= 26:                               ##if character is a lowercase letter
if char in alphabet[x]:
res += '%d' % int(ord(char) - 96)       ##add corresponding number to string
if char not in punct:
res += ' '
x += 1
elif 26 < x <= 52:                              ##if character is an uppercase letter
if char in alphabet[x]:
res += '%d' % int(ord(char) - 64)
if char not in punct:
res += ' '
x += 1
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:                                  ##if negative number entered
global res
global original
res += '-'
original = -int(original)
try:
int(original)
except ValueError:
x = 0
for char in original:
if char in punct:
res += char
x += 1
else:
asOctal = "%o" % int(original)
for char in asOctal:
res += str(binary[char])
if int(original) <= 0:
res += '-'
original = -int(original)
asHex = "%X" % int(original)
res += asHex
elif whichconv in('3','Text','text','T','t'):
if int(original) == 0:
res += '%s' % chr(int(original) + 32)
elif 0 < int(original) <= 26: ##if the number is between 8 and 27 (the lowercase letters of the alphabet list
res += '%s' % chr(int(original) + 96)
elif 26 < int(original) <= 52:
res += '%s' % chr(int(original) - 64)
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

allconv = ''
res = ''
again = True    ##beginning condition for loop
while again:    ##starts loop
res = ''    ##makes an empty list so all the conversions will work
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
##3: Decimal
##""")
##        if whichconv in('1','Binary','binary','Bin','bin','B','b'):
##            convertxtbin()
##            print ''.join(res)
##            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
3: Decimal
""")    ##what conversion?
if whichconv in('1','Binary','binary','Bin','bin','B','b'): ##if binary
convertxtbin()  ##convert text to binary
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
3: Text
""")    ##which conversion?
str(original)   ##convert numbers back to a string
convertnum()    ##convert numbers to whatever
print res
allconv += res + '_____'
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
callconv = raw_input("Would you like to see all the conversions made during this session [y/n]? ")
if callconv in('y','Y','1','yes','Yes'):
print allconv
else:
pass
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
```