python - Decrypting Input? -


this assignment:

write program decrypts secret messages.

it should first prompt user scrambled alphabet. should ask secret message. finally, outputs unscrambled version.

note there 26 characters input scrambled alphabet. alphabetic characters translated decoded equivalents (which take while loop), , other, non-alphabetic characters should output without translation.

this code far:

decrypt = ["*"] * 26  scram_alphabet = input("please input scrambled alphabet in order: ")  while len(scram_alphabet) != 26:     scram_alphabet = input("please input scrambled alphabet in order. alphabet must have 26 characters: ")  num = 0  each_letter in scram_alphabet:     decrypt[num] = ord(each_letter)     num = num + 1  print()  print("your scrambled alphabet is: ", end = "")  num in range (26):     print(chr(decrypt[num]), end = "")  print() print()  msg = input("now input scrambled message: ")  print() print()  alphabet = 65  s in range (26):     decrypt[s] = (alphabet)     alphabet = alphabet + 1  print("the unscrambled alphabet is: ", end = "")   num in range (26):     print(chr(decrypt[num]), end = "")  print() print()  print("your unscrambled message reads: ")  alpha in msg.upper():     if alpha < "a" or alpha > "z":         print(alpha, end="")     else:         ord_alpha = ord(alpha)         print (chr(decrypt[ord_alpha - 65]), end = "") 

ex: scrambled alphabet = xqhajdenkltcbzguyfwvmipsor , scrambled message = vnkw kw bo 1wv wjhfjv bjwwxej!

everything works fine until last print statement, says unscrambled message same scrambled message. know instructions call while loop couldn't figure out how use 1 decode alphabet.

any helpers?

well, has been noted, clobbering decrypt variable. however, not building mapping 'scrambled' alphabet regular 1 / @ all.

decrypting without using more basic iteration on lists , simple list functions (index()) might this:

cleartext = "" c in msg:     if c in alphabet:         pos = alphabet.index(c)         cleartext += string.ascii_uppercase[pos]     else:         cleartext += c 

rather patching code, benefit improvement. gather homework , not expected go far, imo there nothing wrong learning anyway.

  • you not checking input alphabet contains whatever consider legal values (e.g. a-z in case), nor checking duplicates. user input old rubbish , break program otherwise.
  • your printing , looping not idiomatic.
  • functions breaking code more readable , maintainable pieces.
  • this may come across old school or pedantic, lines longer 80 characters not recommended style python. adjacent string literals (e.g "one""two") joined (even across newlines).

if had you're doing without translate (see below), might (just quick example, improved bit of work):

import string  def validate(alpha):     # 26 characters long?     if len(alpha) != 26: return false     c in alpha:         # every character in [a-z]?         if c not in string.ascii_uppercase: return false         # character duplicated?         if alpha.count(c) > 1: return false     return true   alphabet = "" while not validate(alphabet):     alphabet = input("please input encryption alphabet in order (only a-z"                      " allowed, no duplicates): ")  msg = input("now input encrypted message: ")  print("your encrypted alphabet is:", alphabet) print("your encrypted message is:", msg)  # create mapping 1 alphabet other using dictionary table = dict(zip(alphabet, string.ascii_uppercase)) cleartext = "".join([table[c] if c in table else c c in msg])  print("your decrypted message reads:", cleartext) 

lastly, using python's builtin string translation, so:

import string # read , validate alphabet # read message print(str.translate(message, str.maketrans(alphabet, string.ascii_uppercase))) 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -