This question already has answers here:
How to print without newline or space?
                                
                                    (21个回答)
                                
                        
                                4年前关闭。
            
                    
我的代码从用户那里获取一行文本,并尝试使用我创建的功能对文本进行编码或解码。但是,我试图打印一行的所有结果,并且还包括用户输入的空格,以便清楚地显示每个单词都已编码。当前,它仅将所有结果打印在彼此之间,并且不包括用户输入的空格。

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)

最佳答案

构建一个字符串,而不是一次打印一个字符:

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

10-06 09:02
查看更多