Sunday, 15 July 2012

concatenation - Python False Concatenate Error? -


I can not find my fault I'm trying to write a simple program that uses messages by using the cursor change method Encrypts However, I am getting an imaginary error as follows:

  alphabet = {"a": 0, "b": 1, "c": 2, "d": 3, "E": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11, "m 15, "q": 16, "r": 17, "s": 18, "t": 19, "U": 12, "n": 13, 'o': 14, "p": 15; 20, "V": 21, "W": 22, "x": 23, "y": 24, "z": 25} alpha2 = dict (zip (alphabet.values ​​(), alphabet.keys ())) Def key (n): code = code in [i] = alpha2 [code [i]] return code def encode (x, n): my_key = code for i = code (i) + n ) = Key (n) message = [] for me in x: message.append (my_key [i]) Change the sign key (13) in print sign ("message", 13)  

I think it's absurd because my . / Caesars.py After running,

  {'a': 'n', 'c': 'p', 'b': 'o', 'e': 'r', 'd' 'Q', 'g': 't', 'f': 's',' i ':' v ',' h ':' u ',' k ':' x ',' j ':' w 'A', 'Q': 'D', 'P': 'C', '' '' '' '' '' '' '' '' '' 'S': 'F', 'R': 'E', 'U': 'H', 'T': 'G', 'W': 'J', 'V': 'I', 'Y ':' L ',' X ':' K ',' Z ':' m '} Traceback last date for the latest call): Il "./caesars.py", line 56, & lt; Module & gt; Print encode ("Message", 13) Enter the file "./caesars.py", in line 27, my_key = Key (n) file "./caesars.py", line 15, in the key code [I] = (code [I] + n)% 26 type error: 'str' and 'int' can not contain objects  

which clearly means that Key function was successful, but when trying to call the encode function key function, the second time The decision Sya? I have printed N types, and they come out as int. I'm sure this is an inspection in my code, but for my life I am not getting this.

code code

You probably expected

  code>. It does not matter what happens, this line evaluates the  alphabet  variable, produces the reference of a word, and then  code  refers to that same word The settings for the objects of  code  will also change the  alphabet , since both variables are names for the same word, in particular,  alphabet2 [code [i]]   

has the alphabet in the string.

If you want a copy, you can make a copy:

  code = alphabet.copy ()  

Although a copy of alphabet is code .


No comments:

Post a Comment