这是我的代码:
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它将循环回到如果我将它转换为1.
答案 0 :(得分:0)
您需要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
反过来我们 - 转移:
inp = "a"
shift = 4
print(ascii_lowercase[(ascii_lowercase.index(inp) - shift) % 26])
w