gpt4 book ai didi

python - 我正在尝试使用驼峰命名的四个短语

转载 作者:行者123 更新时间:2023-12-05 08:22:06 25 4
gpt4 key购买 nike

这里是 Python 的 super 初学者。到目前为止,这是我的代码:

def make_camel_case(word_string):
word_list = word_string.split(’ ’)
output = ’’
for word in word_list:
word_upper = word[0].upper()
output = output+word_upper
return(output)
def camel_case():
phrase1 = ’purple people eater’
phrase2 = ’i can\’t believe it\’s not butter’
phrase3 = ’heinz 57 sauce’
print(make_camel_case(phrase1))
print(make_camel_case(phrase2))
print(make_camel_case(phrase3))
camel_case()

这是我想要的输出:

purplePeopleEater
iCan’tBelieveIt’sNotButter
heinz57Sauce

我的主要错误信息是invalid character in identifier in line 2

编辑后我的代码运行正常,但输出:

 PPE
ICBINB
H5S

最佳答案

简单易行,只需使用 capitalize()功能:

def make_camel_case(word_string):
word_list = word_string.split(' ')
output = ''

for word in word_list:
word_upper = word.capitalize()
output += word_upper
return output
def camel_case():
phrase1 = 'purple people eater'
phrase2 = "i can't believe it’s not butter"
phrase3 = 'heinz 57 sauce'
print(make_camel_case(phrase1))
print(make_camel_case(phrase2))
print(make_camel_case(phrase3))
camel_case()

关于python - 我正在尝试使用驼峰命名的四个短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65363650/

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