Python 3.63、Vigenere cypher打印超出范围
Python 3.63, Vigenere cypher prints more than the range
第一个问题!在提问之前,我试着四处寻找答案,但除了完全不同的代码,我找不到其他任何东西……:\
我的代码如下:
import sys
if len(sys.argv) != 2:
print("usage: python vigenere.py key")
exit(1)
key = (sys.argv[1])
s = input("plaintext: ")
j = 0
for i in range(len(s)):
# so that j wraps around
j = j % len(key)
for j in range(len(key)):
# check for every j in key if it is upper, or lowercase
if ord(key[j]) >= ord("A") and ord(key[j]) <= ord("Z"):
j -= 65
elif ord(key[j]) >= ord("a") and ord(key[j]) <= ord("z"):
j -= 97
# for every capital letter, print out the encyphered letter
if ord(s[i]) >= ord("A") and ord(s[i]) <= ord("Z"):
print("1{}".format(chr((ord(s[i]) + j - 65) % 26 + 65), end=''))
# same for every non capital
elif ord(s[i]) >= ord("a") and ord(s[i]) <= ord("z"):
print("2{}".format(chr((ord(s[i]) + j - 97) % 26 + 97), end=''))
# if it is not capital, print it out
else:
print("3{}".format(s[i]), end='')
它是一个vigenere cypher,它需要一个密钥,和一个明文,并用密钥对明文进行加密。当我 运行:
>python vigenere.py abc
plaintext: abc
I get:
2h
2i
2j
2i
2j
2k
2j
2k
2l
(我把 1、2 和 3 放在那里,这样我就可以看到资本和非资本是否有效。)
所以我的问题是,换行符从哪里来?我在每次打印后加上 =end''。
另外,我看不到它如何在范围内打印。我尝试寻找解决方案,但其中包含的代码与我的完全不同。任何人都对我的问题有提示,
移动括号,在您的代码中 end=''
用于 format
函数:
print("1{}".format(chr((ord(s[i]) + j - 65) % 26 + 65), end=''))
至
print("1{}".format(chr((ord(s[i]) + j - 65) % 26 + 65)), end='')
并在第二个后缩进 for
http://rextester.com/VOVSP83705
import sys
key = "key"
s = "abc"
j = 0
for i in range(len(s)):
# so that j wraps around
j = j % len(key)
for j in range(len(key)):
# check for every j in key if it is upper, or lowercase
if ord(key[j]) >= ord("A") and ord(key[j]) <= ord("Z"):
j -= 65
elif ord(key[j]) >= ord("a") and ord(key[j]) <= ord("z"):
j -= 97
# for every capital letter, print out the encyphered letter
if ord(s[i]) >= ord("A") and ord(s[i]) <= ord("Z"):
print("{}".format(chr((ord(s[i]) + j - 65) % 26 + 65)), end='')
# same for every non capital
elif ord(s[i]) >= ord("a") and ord(s[i]) <= ord("z"):
print("{}".format(chr((ord(s[i]) + j - 97) % 26 + 97)), end='')
# if it is not capital, print it out
else:
print("{}".format(s[i]), end='')
第一个问题!在提问之前,我试着四处寻找答案,但除了完全不同的代码,我找不到其他任何东西……:\ 我的代码如下:
import sys
if len(sys.argv) != 2:
print("usage: python vigenere.py key")
exit(1)
key = (sys.argv[1])
s = input("plaintext: ")
j = 0
for i in range(len(s)):
# so that j wraps around
j = j % len(key)
for j in range(len(key)):
# check for every j in key if it is upper, or lowercase
if ord(key[j]) >= ord("A") and ord(key[j]) <= ord("Z"):
j -= 65
elif ord(key[j]) >= ord("a") and ord(key[j]) <= ord("z"):
j -= 97
# for every capital letter, print out the encyphered letter
if ord(s[i]) >= ord("A") and ord(s[i]) <= ord("Z"):
print("1{}".format(chr((ord(s[i]) + j - 65) % 26 + 65), end=''))
# same for every non capital
elif ord(s[i]) >= ord("a") and ord(s[i]) <= ord("z"):
print("2{}".format(chr((ord(s[i]) + j - 97) % 26 + 97), end=''))
# if it is not capital, print it out
else:
print("3{}".format(s[i]), end='')
它是一个vigenere cypher,它需要一个密钥,和一个明文,并用密钥对明文进行加密。当我 运行:
>python vigenere.py abc
plaintext: abc
I get:
2h
2i
2j
2i
2j
2k
2j
2k
2l
(我把 1、2 和 3 放在那里,这样我就可以看到资本和非资本是否有效。)
所以我的问题是,换行符从哪里来?我在每次打印后加上 =end''。 另外,我看不到它如何在范围内打印。我尝试寻找解决方案,但其中包含的代码与我的完全不同。任何人都对我的问题有提示,
移动括号,在您的代码中 end=''
用于 format
函数:
print("1{}".format(chr((ord(s[i]) + j - 65) % 26 + 65), end=''))
至
print("1{}".format(chr((ord(s[i]) + j - 65) % 26 + 65)), end='')
并在第二个后缩进 for
http://rextester.com/VOVSP83705
import sys
key = "key"
s = "abc"
j = 0
for i in range(len(s)):
# so that j wraps around
j = j % len(key)
for j in range(len(key)):
# check for every j in key if it is upper, or lowercase
if ord(key[j]) >= ord("A") and ord(key[j]) <= ord("Z"):
j -= 65
elif ord(key[j]) >= ord("a") and ord(key[j]) <= ord("z"):
j -= 97
# for every capital letter, print out the encyphered letter
if ord(s[i]) >= ord("A") and ord(s[i]) <= ord("Z"):
print("{}".format(chr((ord(s[i]) + j - 65) % 26 + 65)), end='')
# same for every non capital
elif ord(s[i]) >= ord("a") and ord(s[i]) <= ord("z"):
print("{}".format(chr((ord(s[i]) + j - 97) % 26 + 97)), end='')
# if it is not capital, print it out
else:
print("{}".format(s[i]), end='')