[Tutor] printing without a new line

Dragonfirebane at aol.com Dragonfirebane at aol.com
Sat Jun 12 18:40:32 EDT 2004


Hello all,

Thanks to all those who helped me with the last problem i had with this program, and that section now works beautifully, aside from the fact that a new line is printed at the end of each character conversion. Thus, "hello o my" yields:

1000 
101 
1100 
1100 
1111
  
1111 
  
1101 
11001

using the following code:

def convertxt():
    x = 0
    while x <= 26:
        if char in alphabet[x]:
            print binary[x] + ' '
        x += 1
def convertnum():
    whichconv = raw_input("""Convert to:
1: Binary
2: Hexadecimal
""")
    if whichconv in('1','Binary','binary','bin','b'):
        if char in number:
            print binary[int(original) - 1]
    elif whichconv in('2','Hexadecimal','hexadecimal','hexadec','hex','h'):
        if char in number:
            print hexadec[int(original) - 1]


import time

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']
binary = [' ','01','10','11','100','101','110','111','1000','1001','1010','1011','1100','1101','1110','1111','10000','10001','10010','10011','10100','10101','10110','10111','11000','11001','11010','11011','11100','11101','11110','11111','100000','100001','100010','100011','100100','100101','100110','100111','101000','101001','101010','101011','101100','101101','101110','101111','110000','110001','110010','110011','110100','110101','110110','110111','111000','111001','111010','111011','111100','111101','111110','111111','1000000','1000001','1000010','1000011','1000100','1000101','1000110','1000111','1001000','1001001','1001010','1001011','1001100','1001101','1001110','1001111','1010000','1010001','1010010','1010011','1010100','1010101','1010110','1010111','1011000','1011001','1011010','1011011','1011100','1011101','1011110','1011111','1100000','1100001','1100010','1100011','1100100','1100101','1100110','1100111','1101000','1101001','1101010','1101011','1101100','1101101','1101110','1101111','1110000','1110001','1110010','1110011','1110100','1110101','1110110','1110111','1111000','1111001','1111010','1111011','1111100','1111101','1111110','1111111','10000000','10000001','10000010','10000011','10000100','10000101','10000110','10000111','10001000','10001001','10001010','10001011','10001100','10001101','10001110','10001111','10010000','10010001','10010010','10010011','10010100','10010101','10010110','10010111','10011000','10011001','10011010','10011011','10011100','10011101','10011110','10011111','10100000','10100001','10100010','10100011','10100100','10100101','10100110','10100111','10101000','10101001','10101010','10101011','10101100','10101101','10101110','10101111','10110000','10110001','10110010','10110011','10110100','10110101','10110110','10110111','10111000','10111001','10111010','10111011','10111100','10111101','10111110','10111111','11000000','11000001','11000010','11000011','11000100','11000101','11000110','11000111','11001000','11001001','11001010','11001011','11001100','11001101','11001110','11001111','11010000','11010001','11010010','11010011','11010100','11010101','11010110','11010111','11011000','11011001','11011010','11011011','11011100','11011101','11011110','11011111','11100000','11100001','11100010','11100011','11100100','11100101','11100110','11100111','11101000','11101001','11101010','11101011','11101100','11101101','11101110','11101111','11110000','11110001','11110010','11110011','11110100','11110101','11110110','11110111','11111000','11111001','11111010','11111011','11111100','11111101','11111110','11111111']
number = ['0','1','2','3','4','5','6','7','8','9']
hexadec = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','10','11','12','13','14','15','16','17','18','19','1A','1B','1C','1D','1E','1F','20','21','22','23','24','25','26','27','28','29','2A','2B','2C','2D','2E','2F','30','31','32','33','33','34','35','36','37','38','39','3A','3B','3C','3D','3E','3F','40','41','42','43','44','45','46','47','48','49','4A','4B','4C','4D','4E','4F','50','51','52','53','54','55','56','57','58','59','5A','5B','5C','5D','5E','5F','60','61','62','63','64','65','66','67','68','69','6A','6B','6C','6D','6E','6F','70','71','72','73','74','75','76','77','78','79','7A','7B','7C','7D','7E','7F','80','81','82','83','84','85','86','87','88','89','8A','8B','8C','8D','8E','8F','90','91','92','93','94','95','96','97','98','99','9A','9B','9C','9D','9E','9F','A1','A2','A3','A4','A5','A6','A7','A8','A9','AA','AB','AC','AD','AE','AF','B0','B1','B2','B3','B4','B5','B6','B7','B8','B9','BA','BB','BC','BD','BE','BF','C0','C1','C2','C3','C4','C5','C6','C7','C8','C9','CA','CB','CC','CD','CE','CF','D0','D1','D2','D3','D4','D5','D6','D7','D8','D9','DA','DB','DC','DD','DE','DF','E0','E1','E2','E3','E4','E5','E6','E7','E8','E9','EA','EB','EC','ED','EE','EF','F0','F1','F2','F3','F4','F5','F6','F7','F8','F9','FA','FB','FC','FD','FE','FF']
again = True
while again:
    original = raw_input("Please enter numbers or text to be converted. ")
    for char in original:
        try:
            int(original)
        except TypeError:
            convertxt()
        except ValueError:
            convertxt()
        else:
            if char in number:
                convertnum()
    more = raw_input("Would you like to convert more text or numbers [y/n]? ")
    if more in 'Yy':
        pass
    elif more in 'Nn':
        again = False
print "Thank you for using Multivert.  Multivert will now close"
time.sleep(1.1)
import sys
sys.exit()

Any ideas about how to prevent the program from beginning a new line with each character would be appreciated. Also, to address the issue of a triple-space appearing between binary 'words' (a single space appears between letters of a word) once the new line issue has been resolved (due to:

        if char in alphabet[x]:
            print binary[x] + ' '

and

alphabet = [' ', . . .]
binary = [' ', . . .] ##a space in text converts to a space in
                      ##binary, so that 'a bc' converts to
                      ##'01   10 11'
                                    ), i will change binary so that a space in text is converted to an underscore in binary, meaning that 'a bc' is converted to '01 _ 10 11'.


"n thats the way the cookie crumbles"

America's Favorite Cookie


More information about the Tutor mailing list