【问题标题】:How do I finish this program for a Caeser cipher? [closed]我如何完成这个凯撒密码程序? [关闭]
【发布时间】:2018-01-15 18:39:01
【问题描述】:

我正在开发一个可以破译凯撒密码的程序。当我运行程序时没有输出。提前谢谢!

这是我的代码

def test():
    value = input("Value here!") 
    with open ("cipher.*txt") as f:

        nice_strings = []
        for line in f:
            line = line.strip() 
            nice_str_chars = []

            for char in line:

                int_of_char = ord(char)

                int_of_char += value 
                nice_char = chr(int_of_char)

                nice_str_chars.append(nice_char)

            nice_str = ''.join(nice_str_chars)

            nice_strings.append(nice_str)

            print (line, '=>', nice_str, '\n')

        return nice_strings

【问题讨论】:

  • StackOverflow 不是代码编写服务。再次发帖前请阅读help center
  • 您几乎可以肯定没有名为 cipher.*txt 的文件。
  • 提示:添加需要更多工作。但看来你走对了。
  • @zyflair 这是一个随机的想法:在 stackoverflow 上,以“帮助我完成作业”作为开始的问题通常被认为是错误的形式。社区通常更接受:保持简单:here is my code, here is my input, here is my output, here is my expected output, can you help me understand why I don't get the expected output 这种方法似乎得到了更多的积极关注,并导致更少的反对票。即使您正在编写的代码是为了家庭作业。 = )

标签: python python-3.x encryption


【解决方案1】:

我突然想到了几个问题:

在您提供的代码中,从未调用过此函数。

文件名似乎有问题

需要将输入值转换为整数,以便进行数学运算...

def test():
    ### convert the input to an integer
    value = int(input("Value here!"))

    ### presuming you want 'cipher.txt' versus 'cipher.*txt'
    with open ("cipher.txt") as f:      

        nice_strings = []
        for line in f:
            line = line.strip() 
            nice_str_chars = []

            for char in line:
                int_of_char = ord(char)
                int_of_char += value 
                nice_char = chr(int_of_char)
                nice_str_chars.append(nice_char)

            nice_str = ''.join(nice_str_chars)
            nice_strings.append(nice_str)
            print (line, '=>', nice_str, '\n')

        return nice_strings

### don't forget to call your function:

test()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2020-05-31
    相关资源
    最近更新 更多