gpt4 book ai didi

python - 类型错误 : translate() takes exactly one argument (2 given)

转载 作者:行者123 更新时间:2023-11-28 21:08:05 25 4
gpt4 key购买 nike

<分区>

我的代码在 python 2.x 版本上运行良好,但是当我试图在 python 3.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 <module>
File "python", line 12, in sms_encoding
TypeError: translate() takes exactly one argument (2 given)

为什么 translate() 不起作用?有任何替代解决方法吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com