酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

写出一段python代码,要求如下: 描述 使用凯撒密码将字符串中的小写字母转化为后移n位(ascii码后移n位)的字母,如a后移3位(ascii码后移n位)即为d,注意z后移3位应该为c,其他字符不变 字符串转ascii码的函数是ord(),如ord('a')的值为97,ascii码转字符串的函数是chr(97)的值为'a' 输入 一行一个字符串,表示明文ming 一行一个整数,表示凯撒密码转换时ascii码需要后移的位数n 输出

```python def caesar_cipher(ming, n): cipher_text = '' for char in ming: if char.islower(): new_char = chr((ord(char) - ord('a') + n) % 26 + ord('a')) else: new_char = char cipher_text += new_char return cipher_text ming = input() n = int(input()) result = caesar_cipher(ming, n) print(result) ``` [2024-12-16 16:21:46 | AI写代码神器 | 243点数解答]

相关提问