接收和旋转字符的功能-凯撒密码 [英] Function That Receives and Rotates Character - Caesar Cipher

查看:49
本文介绍了接收和旋转字符的功能-凯撒密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数

I'm trying to create a function

rotate_character(char, rot)

接收字符"char"(长度为1的字符串)和整数"rot".该函数应返回一个长度为1的新字符串,这是将char按右边的烂位数旋转char的结果.

that receives a character, "char" (a string with a length of 1), and an integer "rot". The function should return a new string with a length of 1, which is the result of rotating char by rot number of places to the right.

因此,对于char,输入"A",对于rot输入"13"将返回

So an input of "A" for char and "13" for rot would return

N

(其中A的初始值为0,B的初始值为1,依此类推).轮换期间应保持大写.

(with A having an initial value of 0, and B having an initial value of 1, etc). Capitalization should be maintained during rotation.

我已经创建了一个函数,该函数通过使用字典返回字母在字母表中的位置:

I already created a function that returns the position of a letter in the alphabet by using a dictionary:

letter = input("Enter a letter: ")

def alphabet_position(letter):
    alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3,
                    'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6,
                    'H':7, 'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10,
                    'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N': 13,
                    'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16,
                    'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19,
                    't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22,
                    'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25 }
    pos = alphabet_pos[letter]
    return pos  

我认为可以使用此函数在旋转之前获取(char)的初始值.

I figure that I can use this function to get the initial value of (char) before rotation.

def rotate_character(char, rot)
    initial_char = alphabet_position(char)
    final_char = initial_char + rot

但是我的问题是,如果initial_char + rot大于25,我需要重新回到字母的开头并继续计数.因此,应返回"w"(初始值为22)的输入+ rot的输入8

But my problem is that, if initial_char + rot is greater than 25, I need to wrap back to the beginning of the alphabet and continue counting. So an input of "w" (initial value of 22) + an input of 8 for rot should return

e

我如何使用python这么说?

How do I say this using python?

if final_char > 25, start at the beginning of the list and continue counting

我是否需要使用我在Alphabet_position函数中创建的字典?也有人建议通过使用Python的内置字母列表来找到字符编号,如下所示:

And do I necessarily need to use the dictionary that I created in the alphabet_position function? It was also suggested that I find the character number by using Python's built-in list of letters, like this:

import string

letter = input('enter a letter: ')

def alphabet_position(letter):
    letter = letter.lower()
    return list(string.ascii_lowercase).index(letter)

return(alphabet_position(letter))

我不确定在计数时必须包装时,哪个是更好的选择.感谢您的帮助/建议!

I'm not sure which one of these is the better option to go with when you have to wrap while you're counting. Thanks for your help / suggestions!

编辑:

现在我的代码如下:

letter = input("enter a letter")
rotate = input("enter a number")

def rotate(letter, rotate):
    letter = letter.lower()
    return chr((ord(letter) + rotate - 97) % 26 + 97)

print(rotate(letter))

编辑2 :

def rotate(letter, number):
    letter = letter.lower()
    shift = 97 if letter.islower() else 65
    return chr((ord(letter) + number - shift) % 26 + shift)

letter = input('Enter a letter: ')
number = int(eval(input('Enter a number: ')
print(rotate(letter, number))

给我一​​个ParseError:"ParseError:第8行输入错误"(打印行)

gave me a ParseError: "ParseError: bad input on line 8" (the print line)

推荐答案

def rotate(letter, rot):
    shift = 97 if letter.islower() else 65
    return chr((ord(letter) + rot - shift) % 26 + shift)

letter = input('Enter a letter: ')
rot = int(input('Enter a number: '))
print(rotate(letter, rot))

这篇关于接收和旋转字符的功能-凯撒密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆