def ULIfrom(in1):
out1 = 0
out2 = ""
out3 = ""
dict2 = {' ':int(0),'a':int(1),'b':int(2),'c':int(3),'d':int(4),'e':int(5),'f':int(6),'g':int(7),'h':int(8),
'i':int(9),'j':int(10),'k':int(11),'l':int(12),'m':int(13),'n':int(14),'o':int(15),'p':int(16),'q':int(17),
'r':int(18),'s':int(19),'t':int(20),'u':int(21),'v':int(22),'w':int(23),'x':int(24),'y':int(25),'z':int(26)
}
for x in in1:
if x == ".":
out2 = dict2.get(out1)
out3 += out2
else:
if x == "U":
out1 += 10
elif x == "L":
out1 += 5
elif x == "I":
out1 += 1
return(out2)
print (ULIfrom(input("enter your encoded phrase:")))
我对编码相当陌生,所以当我尝试在out3变量中存储多个字典值时,我不知道发生了什么,它说您不能组合非类型和字符串,因为输出的字典值应该是字符串
您生成了一个从单个字符串键映射到整数值的字典,但代码的其余部分似乎需要相反的映射—从整数转换为字符
此外,当您将
out2
连接到out3
时,您不会测试它是从字典查找返回的字符串(如果找到键)还是None
值。如果out2 == None
,则应跳过连接,或者为get
调用提供默认字符串值(如果键不在字典中,则返回):不知何故,你的功能结果应该是什么还不清楚。我猜您希望遍历
in1
中的所有字符,并将字典中的值作为总数相加。您可能希望在将来为变量使用更有意义的名称,以便您和其他读者能够了解它们应该包含哪些内容不需要写下映射字典的每个值。写一本字典,让Python帮你完成。字母
'U'
、'L'
和'I'
的大小写可以添加到字典中,因此您不必在代码后面处理它们然后在字符串上循环,从字典中找到条目(默认值为
0
),并添加这些值以获得总数如果替换,则可以进一步缩短此代码
与
澄清问题后,我将代码调整为:
结果是
hello word
现在来看看真正邪恶的东西。我假设输入是有效的,这样代码就短了一点。这是一条单行线:
孩子们,不要在家里这样做强>
感谢大家的帮助,我终于完成了代码
打印(ULIto(输入(“输入您的(仅限小写字母)短语:”))
这是将单词转换为代码的函数
打印(ULIfrom(输入(“输入您的编码短语:”))
此函数用于将代码改回文字
相关问题 更多 >
编程相关推荐