我的代码在Python2.x版本上运行得很好,但是当我试图在Python3.x版本上运行它时,它出现了错误。
主题:需要缩写任何短信编码。
代码:def sms_encoding(data):
#start writing your code here
print(data)
data.split(" ")
data_list=data.split(" ")
sms_encd=[]
final_sms=""
for i in range(len(data_list)):
if data_list[i].lower() in ['a','e','i','o','u']:
sms_encd.append(data_list[i])
elif len(data_list[i])>1:
a = data_list[i].translate(None,'aeiouAEIOU')
sms_encd.append(a)
for j in range(len(sms_encd)):
final_sms += str(sms_encd[j])+" "
return final_sms[:-1]
data="I will not repeat mistakes"
print(sms_encoding(data))
输出:
2.x版本:I will not repeat mistakes
I wll nt rpt mstks
3.x版本:I will not repeat mistakes
Traceback (most recent call last):
File "python", line 18, in
File "python", line 12, in sms_encoding
TypeError: translate() takes exactly one argument (2 given)
为什么translate()不起作用?有其他的解决办法吗?