我的代码从用户那里获取一行文本,并尝试使用我创建的函数对文本进行编码或解码。但是,我试图打印一行的所有结果,并且还包括用户输入的空格,以便清楚地显示每个单词已被编码。它目前只是将所有结果打印在彼此之下,并且不包括用户输入的空格。
print ""
# To God be the Glory
text = raw_input("Please enter a line of text: ")
text = text.lower()
print ""
key = int(input("Please enter a key: "))
def ascii_func (text) :
for charc in text:
if charc in ['-', '+', '*', '/', '!' , "@"]:
print "Error input is not correct"
for charc in text:
if charc != " " :
charc = ord(charc)
charc = (charc - 97) + key
charc = (charc % 26)
charc = charc + 97
charc = chr(charc)
print charc
ascii_func(text)
答案 0 :(得分:1)
构建一个字符串,而不是一次打印一个字符:
result = ''
for charc in text:
if charc != " " :
charc = ord(charc)
charc = (charc - 97) + key
charc = (charc % 26)
charc = charc + 97
charc = chr(charc)
result += charc
print result