why it append one more letter after decode?

ROGER GRAYDON CHRISTMAN dvl at psu.edu
Mon Oct 30 07:14:57 EDT 2017


On Sun, Oct 29, 2017 11:06 PM, Ho Yeung Lee wrote:
>
if run these function to decode in python interactive console,
>it can decode correct, 
>
>but when run with a big project, it append a letter Y
>
>
>On Monday, October 30, 2017 at 9:48:36 AM UTC+8, Ho Yeung Lee wrote:
>> def leftrotate(l, n):
>>     return l[n:] + l[:n]
>> 
>> def rightrotate(l, n):
>>     return l[-n:] + l[:-n]
>> 
>> def encode(s, k, kk):
>>     l = [ord(i) for i in s]
>>     return leftrotate(''.join([chr(i + k) for i in l]), kk)
>> 
>> def decode(s, k, kk):
>>     l = [ord(i) for i in rightrotate(s, kk)]
>>     return ''.join([chr(i - k) for i in l])
>> 
>> 
>> yesterday i add above code and run it with batch file 
>> it can decode a correct password
>> 
>> then i install cx_freeze to produce executable file
>> 
>> but today when i run it, i see the source of encrypted password is correct
>> but the decode one, it append one more letter Y at the end of string
>> 
>> why?
>> is cx_freeze change the source python 2.7?
>
>i discover when 
>it 
>decode("\\",1,2)
>in interactive console
>is [
>
>but decode in big project
>it see as
>decode(r"\\",1,2)
>[[
>
>it double
>
>


My first inclination is that the data could have changed.
For example, obtaining a string from the keyboard using input()
would suppress the carriage return.   But obtaining
a string from a file might preserve the carriage return
at the end of the line.   I could imagine then that the
extra carriage return contributed an extra character to your data.

But then I see you had an example with "\\" in one case,
and r"\\" in the other case.  These are not equal to each other,
and naturally would not give equal results from your function.

So that leads to the second possibility that you are not calling
the function in the same way.

In either case, you cannot blame the function for giving you
different results if you give it different data.

Roger Christman
Pennsylvania State University 



More information about the Python-list mailing list