Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help...
#1
For the love of god, cant understand why does this print the index of 18 for s which it is actually 7...
Would appreciate any help.


def encrypt_atbash(msg):
    alphabet = "abcdefghijklmnopqrstuvwxyz"  #regular alphabet prior to reverse
    new_alphabet = "zyxwvutsrqponmlkjihgfedcba"
    ciphertext = "" #empty tray for uncoded text
    position = []
    for char in msg:
        old_position = new_alphabet.find(char) 
        print(old_position)
        new_cd = alphabet[old_position]
        
        
        #ciphertext.append[old_position]
      
ans = encrypt_atbash('hello world')
Reply
#2
What exactly do you want to do?
Reply
#3
I got it.. I was looking at the wrong key... and thinking it was a decrypt and not encrypt. Anyways, solved.

Thanks for your quick reply. :)
Reply
#4
Some ideas how to improve code:

- there is built-in module string which provides constants including ascii_lowercase

- it's easy to reverse string

>>> import string
>>> letters = string.ascii_lowercase
>>> letters
'abcdefghijklmnopqrstuvwxyz'
>>> letters[::-1]
'zyxwvutsrqponmlkjihgfedcba'
- you have 'chipertext' and 'position' which are assingned but never used

- your function doesn't return anything explicitly (it implicitly returns None) so I wonder what problem you actually solved
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020