作业:
编写一个脚本,输入一行加密文本和一个距离值,然后使用Caesar密码输出明文。在
我的问题:
正在打印hello ^3 world ^2
我不太清楚为什么。如果您能帮助我修复或理解代码中的错误,我将不胜感激。(这不是为等级分配的,即没有为我自己的利益而设定的到期日)
它希望我能够使用的输入及其各自的:
Lipps${svph%
4
Jhss'tl'Pzothls5
7
Zu&hk2&ux&tuz&zu&hk
2
khoor#zruog$
3
我的代码:
code = input("enter coded text: ")
distance = int(input("enter value: "))
plainText = ""
for ch in code:
ordvalue = ord(ch)
ciphervalue = ordvalue - distance
if ciphervalue < ord('a'):
ciphervalue = ord('z') - \
(distance - (ord('a')-ordvalue - 1))
plainText += chr(ciphervalue)
print(plainText)
首先,您应该更容易地将所有输入与输出分离,因为:
现在,你有:
^{pr2}$我猜你是在期待“你好,世界!”。如果你去掉这两条线你就会得到:
看:
这是问题的有趣部分。但是我认为你有一个正确的直觉:当产生的
ordvalue
值不在预期范围内(即负的或大的)时会发生什么?在函数
ord
生成一个unicode码位,介于0和1114111之间,但我认为对于练习,您可以将范围限制在0-127(ASCII字符):注意:
相当于:
如果只希望有可打印字符,可以使用
string
模块:相关问题 更多 >
编程相关推荐