[Tutor] Loops

orbitz orbitz at ezabel.com
Thu Jul 1 10:10:33 EDT 2004


I'm a little confused about what you are actually trying to accomplish 
here.  You are representing some things in binary and some not (spaces, 
punctuation?).  I'm not sure why you have the caret to begin with, 
uppercase letters would simply have a different code than their 
lowercase counterpart.

Dragonfirebane at aol.com wrote:

> As part of a program I'm working on, Binary input is taken in 
> (punctuation included) and text is output-ed. However, to symbolize 
> capital letters in Binary, a carat (^) is inserted, which makes it 
> necessary to inclue the part from "else" - down.  What isn't shown 
> here is that the entire section is under a for loop: for x in 
> original. Original is the binary raw_input. m begins as 0. Therefore, 
> my problem is that after removing the carat and adding enough to the 
> number that it will be considered uppercase (done by segment from 
> "else" - down), even though m stays the same due to m -= 1 and m += 
> 1, meaning that that part of original will be re-processed, there are 
> still only a limited number of 'x's in original. Therefore, what 
> should come out as:
>  
> "'ello Sue. I've got legs!"
>  
> comes out as:
>  
> "'ello Sue 've got legs"
>  
> from:
>  
> " ' 101 001100 001100 001111 000 ^010011 010101 101 . 000 ^001001 ' 
> 010110 101 000 111 001111 010100 000 001100 101 111 010011 ! "
>  
> most of the relevant code is below. for the full work-in-progress 
> program (approx. 500 lines), open attachment. the relevant module is 
> 'convertbin(whichconv)' (line 118) and the global variables are 
> defined approx. from lines 368-395
>  
>  
>             try:
>                 asNum = str(long(str(original[m]),2))
>                 if int(asNum) == 0:
>                     res += '%s' % chr(int(asNum) + 32)
>                 elif 0 < int(asNum) <= 26:
>                     res += '%s' % chr(int(asNum) + 96)
>                 elif 26 < int(asNum) <= 52:
>                     res += '%s' % chr(int(asNum) + 38)
>             except ValueError:
>                 global original
>                 if x in punct:
>                     res += x
>                 else:
>                     for char in x:
>                         if char == '^':
>                             x = ' '.join(x)
>                             x = x.split()
>                             x.remove(char)
>                             x = ''.join(x)
>                             asNum = int(long(str(x),2))
>                             asNum += 26
>                             asOctal = "%o" % int(asNum)
>                             original[m] = ''
>                             for char in asOctal:
>                                 original[m] += str(binary[char])
>           nbsp;                   asNum = int(long(str(x),2))
>                             asNum += 26
>                             asOctal = "%o" % int(asNum)
>                             original[m] = ''
>                             for char in asOctal:
>                                 original[m] += str(binary[char])
>                             original[m] = str(original[m])
>                             m -= 1
>             m += 1
> any suggestions for how I can reprocess binary minus the carat without 
> 'wasting' one of the x's (characters) of original would be vastly 
> appreciated.
>  
> Thanks in advance,
> Orri
>  
> Email: dragonfirebane at aol.com
> AIM: singingxduck
> Programming Python for the fun of it.
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>  
>




More information about the Tutor mailing list