如何将用户输入变成单个变量?

时间:2019-11-22 03:16:29

标签: python python-3.x encoder

我想做的是制作一个测试编码器,但是我无法将用户的输入转换成各个变量,这些变量中包含用户的各个文本字母。我该怎么做?我在这个网站上尝试了许多不同的答案,但是都没有用。这是我的代码:

encode = {
  "!" : "!","?" : "?","." : ".","," : ","," " : " ","A" : "7","B" : "8",
  "C" : "9","D" : "4","E" : "5","F" : "6","G" : "1","H" : "2","I" : "3",
  "J" : "÷","K" : "*","L" : "-","M" : "+","N" : "(7)","O" : "(8)", "P" : "(9)",
  "Q" : "(4)","R" : "(5)","S" : "(6)","T" : "(1)", "U" : "(2)","V" : "(3)",
  "W" : "(÷)","X" : "(*)","Y" : "(-)", "Z" : "(+)","a" : "7","b" : "8",
  "c" : "9","d" : "4","e" : "5","f" : "6","g" : "1","h" : "2","i" : "3",
  "j" : "÷","k" : "*","l" : "-","m" : "+","n" : "(7)","o" : "(8)","p" : "(9)",
  "q" : "(4)","r" : "(5)","s" : "(6)","t" : "(1)","u" : "(2)","v" : "(3)",
  "w" : "(÷)","x" : "(*)","y" : "(-)","z" : "(+)"
}

my_input = input("Type you mesage to be encoded: ")
output = "".join(str(encode[c]) for c in my_input) 
print (output)

1 个答案:

答案 0 :(得分:0)

您应该使用字典而不是单个变量。例如

encode = {
  "A" : 0,
  "B" : 1
}

然后您可以使用字符串连接器来翻译输入

output = "".join(str(encode[c]) for c in my_input)