2024-04-27 02:40:36 发布
网友
这是我的代码:
Alpha =input ("How much would you like to add to the letter 1-10?") Bravo =ord (input("What letter would you like to encrypt?")) + int (Alpha) Charlie =chr (Bravo) print(Charlie)
我试着让它,如果我选择z,它会循环回a,如果我把它移到1。在
您需要modulo运算符,获取字母表中字母的索引,然后执行(index + shift)模26循环字母以避免索引错误:
modulo
(index + shift)
26
from string import ascii_lowercase inp = "z" shift = 1 print(ascii_lowercase[(ascii_lowercase.index(inp) + shift) % 26]) a inp = "w" shift = 4 print(ascii_lowercase[(ascii_lowercase.index(inp) + shift) % 26]) a
要进行反向操作,请执行以下操作:
您需要
modulo
运算符,获取字母表中字母的索引,然后执行(index + shift)
模26
循环字母以避免索引错误:要进行反向操作,请执行以下操作:
^{pr2}$相关问题 更多 >
编程相关推荐