如何将用户的输入转换为单个变量?
How to I turn a user's input into individual variables?
我想做的是制作一个测试编码器,但我无法将用户的输入转换为包含用户个人文本字母的个人变量。我该怎么做?我在这个网站上尝试了很多不同的答案,但其中 none 有效。这是我的代码:
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)
您应该使用字典而不是单个变量。例如
encode = {
"A" : 0,
"B" : 1
}
然后您可以使用字符串连接器来翻译输入
output = "".join(str(encode[c]) for c in my_input)
我想做的是制作一个测试编码器,但我无法将用户的输入转换为包含用户个人文本字母的个人变量。我该怎么做?我在这个网站上尝试了很多不同的答案,但其中 none 有效。这是我的代码:
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)
您应该使用字典而不是单个变量。例如
encode = {
"A" : 0,
"B" : 1
}
然后您可以使用字符串连接器来翻译输入
output = "".join(str(encode[c]) for c in my_input)