在遇到这个小问题之前,我正试图完成一项作业。
我的困境是:我的输出打印正确,但我如何让键 # 及其各自的输出整齐地打印在一起?
示例:
键 1:ABCDEB
关键点 2:EFGFHI
等
我的代码:
def main():
# hardcode
phrase = raw_input ("Enter the phrase you would like to decode: ")
# 1-26 alphabets (+3: A->D)
# A starts at 65, and we want the ordinals to be from 0-25
# everything must be in uppercase
phrase = phrase.upper()
# this makes up a list of the words in the phrase
splitWords = phrase.split()
output = ""
for key in range(0,26):
# this function will split each word from the phrase
for ch in splitWords:
# split the words furthur into letters
for x in ch:
number = ((ord(x)-65) + key) % 26
letter = (chr(number+65))
# update accumulator variable
output = output + letter
# add a space after the word
output = output + " "
print "Key", key, ":", output
main()
请您参考如下方法:
如果我没理解错的话,你需要在每个循环中重置output,并在每个循环中重置print,所以改变:
output = ""
for key in range(0,26):
## Other stuff
print "Key", key, ":", output
到:
for key in range(0,26):
output = ""
## Other stuff
print "Key", key, ":", output
旧结果:
Key 25 : MARK NBSL ... KYPI LZQJ
新结果:
Key 0 : MARK
Key 1 : NBSL
#etc
Key 24 : KYPI
Key 25 : LZQJ
