gpt4 book ai didi

python - 如何为字符串中的每个字母分配一个值,然后将这些值添加到另一个字符串中?”

转载 作者:行者123 更新时间:2023-12-01 09:05:07 24 4
gpt4 key购买 nike

def code_maker():

values = {"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":12, "n":13, "o":14, "p":15, "q":16, "r":17, "s":18, "t":19, "u":20, "v":21, "w":22, "x":23, "y":24, "z":25}

key = input("Please enter a four letter key ").lower()
code = input("Please enter a string ").lower()

added_code = (key[0] + code[0]) + (key[1] + code[1]) + (key[2] + code[2]) + (key[3] + code[3])

我正在尝试做凯撒密码类型的事情,将字符串中的每个字母分配给相应的数字,然后将其值添加到另一个字符串中的另一个字母,但我不知道如何找到关联的每个字母值。

最佳答案

您不想使用已经编写的代码吗?来自 here您可以找到以下内容:

 # Caesar Cipher



MAX_KEY_SIZE = 26



def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')

mode = input().lower()

if mode in 'encrypt e decrypt d'.split():

return mode

else:

print('Enter either "encrypt" or "e" or "decrypt" or "d".')



def getMessage():

print('Enter your message:')

return input()



def getKey():

key = 0

while True:

print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))

key = int(input())

if (key >= 1 and key <= MAX_KEY_SIZE):

return key



def getTranslatedMessage(mode, message, key):

if mode[0] == 'd':

key = -key

translated = ''



for symbol in message:

if symbol.isalpha():

num = ord(symbol)

num += key



if symbol.isupper():

if num > ord('Z'):

num -= 26

elif num < ord('A'):

num += 26

elif symbol.islower():

if num > ord('z'):

num -= 26

elif num < ord('a'):

num += 26



translated += chr(num)

else:

translated += symbol

return translated



mode = getMode()

message = getMessage()

key = getKey()



print('Your translated text is:')

print(getTranslatedMessage(mode, message, key))

它会询问您是否要解密或加密消息。如果要加密,请说 e。然后它会询问 key 号码。对于消息,它会输出消息的加密版本。

关于python - 如何为字符串中的每个字母分配一个值,然后将这些值添加到另一个字符串中?”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52126498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com