简体   繁体   English

为什么我的 python 代码返回换行符?

[英]Why does my python code return a line break?

I am trying to create a binary program in python that can change text to binary and vice versa based on user input.我正在尝试在 python 中创建一个二进制程序,它可以根据用户输入将文本更改为二进制,反之亦然。 So far, it has been going good until I tried encoding the text, and it just returns no string at all.到目前为止,它一直很好,直到我尝试对文本进行编码,它根本不返回任何字符串。 My decoding works, but my encoding returns an empty string.我的解码工作,但我的编码返回一个空字符串。 Please help me debug this.请帮我调试一下。

import json

symbols = [" ", "!", '"', "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@"]
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
upper = list(map(lambda x: x.upper(), letters))
after = ["{", "|", "}", "~"]
symbol_sub = 32
upper_sub = 65
lower_sub = 97
after_sub = 123

choice = input("Do you want to encode text, or decode it? e, or d? ").lower()
if choice == 'e':
    string = input("Type in your text: ")
    allchars = string.split()
    encoded = ''
    for c in allchars:
        if c in symbols:
            i = symbols.index() + symbol_sub
            ibin = bin(i).split('0b')[1]
            encoded += str(" " + ibin)
        elif c in upper:
            i = upper.index() + upper_sub
            ibin = bin(i).split('0b')[1]
            encoded += str(" " + ibin)
        elif c in letters:
            i = lower.index() + lower_sub
            ibin = bin(i).split('0b')[1]
            encoded += str(" " + ibin)
        elif c in after:
            i = after.index() + after_sub
            ibin = bin(i).split('0b')[1]
            encoded += str(" " + ibin)
    print(encoded)

elif choice == 'd':
    string = input("Please input your binary code, each binary string separated by spaces: ")
    allbits = string.split(' ')
    decoded = ''
    for i in allbits:
        b10 = int(i, 2)
        if b10 - after_sub < 0:
            if b10 - lower_sub < 0:
                if b10 - upper_sub < 0:
                    decoded += symbols[b10 - symbol_sub]
                else:
                    decoded += upper[b10 - upper_sub]
            else:
                decoded += letters[b10 - lower_sub]
        else:
            decoded += after[b10 - after_sub]
    print(decoded)

I want to make it so if you typed "A)" it would return "1000001 101001".我想这样做,如果您键入“A)”,它将返回“1000001 101001”。

  1. Use list(string) to get a list of characters, not string.split()使用list(string)获取字符列表,而不是string.split()
  2. lower is not defined, I think you meant letters lower没有定义,我认为你的意思是letters
  3. All the .index() calls are missing an argument (Should be upper.index(c) )所有.index()调用都缺少一个参数(应该是upper.index(c)

After these changes:在这些更改之后:

Do you want to encode text, or decode it? e, or d? e
Type in your text: A)

1000001 101001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM