功能与文本替换

huangapple go评论67阅读模式
英文:

Function with text replacement

问题

I trying to write a "code generator" so I have to replace the letters with symbols. But every time I tried to run the code I got the answer none. How can I fix that? Here is the code:
我正在尝试编写一个“代码生成器”,所以我必须用符号替换字母。但每次我尝试运行代码时,都得到了答案为“无”。我该如何修复这个问题?以下是代码:

codeOrDecode = input ("Wanna code a text or decode? [For decode write d, and for code write c] ")

if codeOrDecode == "c":
textcode = input("What text wanna code? Paste it here: ")
def howToCode (texttobereplaced) :
codedText = textcode.replace(" " , "•")
codedText = textcode.replace("a" , "✡")
codedText = textcode.replace("A" , "✡")
codedText = textcode.replace("b" , "➛")
codedText = textcode.replace("B" , "➛")
codedText = textcode.replace("c" , "▹")
codedText = textcode.replace("C" , "▹")
codedText = textcode.replace("d" , "◷")
codedText = textcode.replace("D" , "◷")
codedText = textcode.replace("e" , "▇")
codedText = textcode.replace("E" , "▇")
codedText = textcode.replace("f" , "Φ")
codedText = textcode.replace("F" , "Φ")
codedText = textcode.replace("g" , "≣")
codedText = textcode.replace("G" , "≣")
codedText = textcode.replace("h" , "➽")
codedText = textcode.replace("H" , "➽")
codedText = textcode.replace("i" , "◦")
codedText = textcode.replace("I" , "◦")
codedText = textcode.replace("j" , "○")
codedText = textcode.replace("J" , "○")
codedText = textcode.replace("k" , "❃")
codedText = textcode.replace("K" , "❃")
codedText = textcode.replace("l" , "◛")
codedText = textcode.replace("L" , "◛")
codedText = textcode.replace("m" , "▲")
codedText = textcode.replace("M" , "▲")
codedText = textcode.replace("n" , "‡")
codedText = textcode.replace("N" , "‡")
codedText = textcode.replace("o" , "▎")
codedText = textcode.replace("O" , "▎")
codedText = textcode.replace("p" , "◉")
codedText = textcode.replace("P" , "◉")
codedText = textcode.replace("q" , "⚂")
codedText = textcode.replace("Q" , "⚂")
codedText = textcode.replace("r" , "Δ")
codedText = textcode.replace("R" , "Δ")
codedText = textcode.replace("s" , "⋮")
codedText = textcode.replace("S" , "⋮")
codedText = textcode.replace("t" , "⅛")
codedText = textcode.replace("T" , "⅛")
codedText = textcode.replace("u" , "▯")
codedText = textcode.replace("U" , "▯")
codedText = textcode.replace("v" , "▧")
codedText = textcode.replace("V" , "▧")
codedText = textcode.replace("w" , "⠓")
codedText = textcode.replace("W" , "⠓")
codedText = textcode.replace("x" , "↬")
codedText = textcode.replace("X" , "↬")
codedText = textcode.replace("y" , "↜")
codedText = textcode.replace("Y" , "↜")
codedText = textcode.replace("z" , "⠽")
codedText = textcode.replace("Z" , "⠽")
print (howToCode(textcode))

How to fix that? I tried every thing I know.
如何修复这个问题?我尝试了我知道的一切。

英文:

I trying to write a "code generator" so I have to replace the letters with symbols. But every time I tried to run the code I got the answer none. How can I fix that? Here is the code:
codeOrDecode = input ("Wanna code a text or decode? [For decode write d, and for code write c] ")


if codeOrDecode == "c":
    textcode = input("What text wanna code? Paste it here: ")
    def howToCode (texttobereplaced) :
        codedText = textcode.replace(" " , "•")
        codedText = textcode.replace("a" , "✡")
        codedText = textcode.replace("A" , "✡")
        codedText = textcode.replace("b" , "➛")
        codedText = textcode.replace("B" , "➛")
        codedText = textcode.replace("c" , "▹")
        codedText = textcode.replace("C" , "▹")
        codedText = textcode.replace("d" , "◷")
        codedText = textcode.replace("D" , "◷")
        codedText = textcode.replace("e" , "▇")
        codedText = textcode.replace("E" , "▇")
        codedText = textcode.replace("f" , "Φ")
        codedText = textcode.replace("F" , "Φ")
        codedText = textcode.replace("g" , "≣")
        codedText = textcode.replace("G" , "≣")
        codedText = textcode.replace("h" , "➽")
        codedText = textcode.replace("H" , "➽")
        codedText = textcode.replace("i" , "◦")
        codedText = textcode.replace("I" , "◦")
        codedText = textcode.replace("j" , "○")
        codedText = textcode.replace("J" , "○")
        codedText = textcode.replace("k" , "❃")
        codedText = textcode.replace("K" , "❃")
        codedText = textcode.replace("l" , "◛")
        codedText = textcode.replace("L" , "◛")
        codedText = textcode.replace("m" , "▲")
        codedText = textcode.replace("M" , "▲")
        codedText = textcode.replace("n" , "‡")
        codedText = textcode.replace("N" , "‡")
        codedText = textcode.replace("o" , "▎")
        codedText = textcode.replace("O" , "▎")
        codedText = textcode.replace("p" , "◉")
        codedText = textcode.replace("P" , "◉")
        codedText = textcode.replace("q" , "⚂")
        codedText = textcode.replace("Q" , "⚂")
        codedText = textcode.replace("r" , "Δ")
        codedText = textcode.replace("R" , "Δ")
        codedText = textcode.replace("s" , "⋮")
        codedText = textcode.replace("S" , "⋮")
        codedText = textcode.replace("t" , "⅛")
        codedText = textcode.replace("T" , "⅛")
        codedText = textcode.replace("u" , "▯")
        codedText = textcode.replace("U" , "▯")
        codedText = textcode.replace("v" , "▧")
        codedText = textcode.replace("V" , "▧")
        codedText = textcode.replace("w" , "⠓")
        codedText = textcode.replace("W" , "⠓")
        codedText = textcode.replace("x" , "↬")
        codedText = textcode.replace("X" , "↬")
        codedText = textcode.replace("y" , "↜")
        codedText = textcode.replace("Y" , "↜")
        codedText = textcode.replace("z" , "⠽")
        codedText = textcode.replace("Z" , "⠽")
    print (howToCode(textcode))

How to fix that? I tried every thing I know.

答案1

得分: 1

你遇到的问题是因为你的 howToCode() 函数没有明确返回任何内容。因此,它隐式地返回了 None。

另外值得一提的是,你可以使用 str.maketransstr.translate 来实现这个功能,从而减少代码量。

以下是一个示例:

a = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
b = '•✡➛▹◷▇Φ≣➽◦○❃◛▲‡▎◉⚂Δ⋮⅛▯▧⠓↬↜⠽'

ENCODE = str.maketrans(a, b)
DECODE = str.maketrans(b, a)

def howToCode(text: str, encode: bool = True) -> str:
    return text.upper().translate(ENCODE if encode else DECODE)

text = input('What text wanna code? Paste it here: ')

encoded = howToCode(text)
print(encoded)
decoded = howToCode(encoded, False)
print(decoded)

控制台输出:

What text wanna code? Paste it here: hello world
➽▇◛◛▎•⠓▎Δ◛◷
HELLO WORLD
英文:

The issue you had was due to the fact that your howToCode() function didn't explicitly return anything. Therefore, it implicitly returned None.

Also worth mentioning that you could do this using str.maketrans and str.translate leading to much less code.

Here's an example:

a = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
b = '•✡➛▹◷▇Φ≣➽◦○❃◛▲‡▎◉⚂Δ⋮⅛▯▧⠓↬↜⠽'

ENCODE = str.maketrans(a, b)
DECODE = str.maketrans(b, a)

def howToCode(text: str, encode: bool = True) -> str:
    return text.upper().translate(ENCODE if encode else DECODE)

text = input('What text wanna code? Paste it here: ')

encoded = howToCode(text)
print(encoded)
decoded = howToCode(encoded, False)
print(decoded)

Console:

What text wanna code? Paste it here: hello world
➽▇◛◛▎•⠓▎Δ◛◷
HELLO WORLD

huangapple
  • 本文由 发表于 2023年6月26日 17:50:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定