2017-12-10 9 views
-1

以下のコードを修正することができず、何らかの理由でコードが実行されていません。あなたはのでそのあなたのコード全体の変数と矛盾しているから別にPythonで 'Encrypt message'を作成する際のコードの脆弱性

message = input ('Enter your message') 

key = int (input('How many Characters should we shift(between 1 - 26)- ')) 

secret-message = "" 
for char in message: 

    if char.isalpha(): 
     #Get the character code and add the shift amount 
     char_code = ord(char) 
     chat_code += key 
     #if upper case then compare to uppercase unicode 
     if char.isupper(): 
      #if upper than Z substract 26 
      if char_code > ord('Z'): 
       char_code -= 26 
      #if smaller than A add 26 
      if char_code < ord('A'): 
       char_cod += 26 
     #Do the same for lower character 
     else: 
      if char_code > ord('z'): 
       char_code -= 26 
      if char_code < ord('a'): 
       char_cod += 26 
     #Convert From Code to letter and add to message 
    secret-message = secret-message + chr(char_code) 
    else: 
     secret-message = secret-message + char 
print (" Encripted :" , secret-message) 
+0

あなたは変数に矛盾しています: 'char_code'、' chat_code'、 'char_cod' – srig

答えて

0

、あなたはsecret-message-演算子を使用している:補正で私を助けてください、そして可能ならば、より良いコーディング練習を示唆し、短いコーディングで助けてくださいSyntaxError: can't assign to operatorを取得しています。これらの補正後、あなたのコードは動作しています:

それを実行
message = input('Enter your message: ') 

key = int(input('How many Characters should we shift(between 1 - 26)- ')) 

secret_message = "" 

for char in message: 
    if char.isalpha(): 
     #Get the character code and add the shift amount 
     char_code = ord(char) 
     char_code += key 
     #if upper case then compare to uppercase unicode 
     if char.isupper():    
      #if upper than Z substract 26 
      if char_code > ord('Z'): 
       char_code -= 26 
      #if smaller than A add 26 
      if char_code < ord('A'): 
       char_code += 26 
     #Do the same for lower character 
     else: 
      if char_code > ord('z'): 
       char_code -= 26 
      if char_code < ord('a'): 
       char_code += 26 
     #Convert From Code to letter and add to message     
     secret_message = secret_message + chr(char_code) 
    else: 
     secret_message = secret_message + char 

print (" Encripted :" , secret_message) 

を:

Enter your message: secret 
How many Characters should we shift(between 1 - 26)- 5 
Encripted : xjhwjy 
>>> 

今、あなたのコードが動作しているとあなたがより良いコーディング練習に興味を持っているので、あなたが投稿したいこと同等のプログラマコードレビューの場合はCode Review Stack Exchangeと同じです。

希望に役立ちます。