简体   繁体   中英

How to make a dictionary loop back to the start in python?

The following code in python is supposed to take a keyword and a message inputted by the user, add the alphabetical values together(so a message of aaa and keyword of bb would equal ccc) I can sum the message and keyword so that it equals 3,3,3 but I don't know how to switch this back to letters.

import string
step = 1
keywful=""
values = dict()
values = {letter: index for index, letter in enumerate(string.ascii_lowercase, 1)}
keyw=input("Enter your keyword for en/decryption")
msg=input("Enter your message for en/decryption")
eord=input("Enter e for encrypt or d for decrypt")
mapkeyw = [values[letter] for letter in keyw]
mapmsg = [values[letter] for letter in msg]
tot = [x + y for x, y in zip(mapkeyw, mapmsg)]
print(tot)

Any help is appreciated. Be advised that I cannot use the chr and ord functions as it needs to be the alphabetical values(a=1,b=2,c=3 etc)

print(''.join([string.ascii_lowercase[i-1] for i in tot]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM