如何打印我的凯撒密码没有空格?

时间:2013-11-15 15:39:43

标签: python printing

我正试图从这个程序中打印出没有空格的信息,但它总是给我每个字母之间的空格。

import string
character = []
message = raw_input('What is your message? ').lower()
shift = raw_input('What is your shift key? ')
type = raw_input('Would you like to cipher(c) or decipher(d)? ')
for character in message:
    if str(type) == 'd':
        number = ord(character) - int(shift)
        if number <= 96:
            number = ord(character) + 26 - int(shift)
    if str(type) == 'c':
        number = ord(character) + int(shift)
        if number >= 122:
            number = ord(character) - 26 + int(shift)
    character = chr(number)
    print(character),

有人知道在一个句子中打印热,以便我可以复制邮件并收件人吗?

3 个答案:

答案 0 :(得分:2)

添加

from __future__ import print_function

到程序的顶部,然后更改

print(character),

print(character, end='')

这是explained here

没有第一行你实际打印(character)这是一个结果为character的表达式,所以它基本上是print character,。使用导入,print ...更改为print(...),并采用额外的参数来提供更多控制权。

请注意,这不适用于非常古老的python版本。你可能需要2.6或更高版本。

[hi scott!]

答案 1 :(得分:1)

在print语句的末尾丢失逗号。当然,那么每个角色都会分开。

可能最好将字符构建成一个可以一次打印的字符串。

答案 2 :(得分:1)

您可以使用sys.stdout.write(character)打印不含空格或换行符的单个字符,就像C putchar一样。

例如:

import string
import sys

character = []
message = raw_input('What is your message? ').lower()
shift = raw_input('What is your shift key? ')
type = raw_input('Would you like to cipher(c) or decipher(d)? ')
for character in message:
    if str(type) == 'd':
        number = ord(character) - int(shift)
        if number <= 96:
            number = ord(character) + 26 - int(shift)
    if str(type) == 'c':
        number = ord(character) + int(shift)
        if number >= 122:
            number = ord(character) - 26 + int(shift)
    character = chr(number)
    sys.stdout.write(character),

print ""