为加密创建循环(Python)

2024-04-27 02:40:36 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

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。在


Tags: theto代码alphayouaddinputlike
1条回答
网友
1楼 · 发布于 2024-04-27 02:40:36

您需要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

要进行反向操作,请执行以下操作:

^{pr2}$

相关问题 更多 >