在经历了很多挫折之后,我制作了我的第一个凯撒解码器:)
但现在的问题是让程序循环。。。在
例如,如果我们想将doge
移动1,没问题,它是ephf
。。。在
但是xyz
,而移位是4???在
所以编程专业人士帮助第一次新手,也就是新手:p 谢谢。。。在
import string
def main():
inString = raw_input("Please enter the word to be "
"translated: ")
key = int(raw_input("What is the key value? "))
toConv = [ord(i) for i in inString] #now want to shift it by key
toConv = [x+key for x in toConv]
#^can use map(lambda x:x+key, toConv)
result = ''.join(chr(i) for i in toConv)
print "This is the final result due to the shift", result
只需将
key
添加到所有实际的字符代码中,那么如果增加的值大于z
,则使用字符代码z
进行模运算,并将其与字符代码a
相加。在我建议使用^{} 。在
因此,我们可以做以下工作:
然后我们可以使用它如下:
^{pr2}$特别是,这不会更改非ascii小写或大写字符的字符,如数字或空格。在
一般来说,要使某个东西“包装”,您需要使用模函数(
%
,在Python中)和要包装的数字和范围。例如,如果我想把数字1到10打印一次,我会这样做:在本例中,这有点困难,因为您使用的是
^{pr2}$ord
,它没有一个很好的“范围”值。如果你做过类似string.ascii_lowercase
的事情,你可以。。。在但是,由于您使用的是ord,我们有点从},因此范围为0->;57(例如
ord('A') == 65
到{range(58)
,常数为65)。换句话说:我们可以改为这样做,但它将包含字符[\]^\
相关问题 更多 >
编程相关推荐