Python 在def函数中将输入转换为首字母缩略词?

Python 在def函数中将输入转换为首字母缩略词?,python,Python,在Python3.14中,如何创建一个函数来将输入转换为首字母缩略词 我已经试过这个了,但我觉得不管用 def acronyms(): words = input("Please enter name: ") acronym = ''.join(word[0] for word in words.upper().split()) return; print (acronyms()) 我感觉你来自MATLAB世界。无论如何,在python中,return返回None(更不

在Python3.14中,如何创建一个函数来将输入转换为首字母缩略词

我已经试过这个了,但我觉得不管用

def acronyms():
    words = input("Please enter name: ")
    acronym = ''.join(word[0] for word in words.upper().split())
    return;
print (acronyms())

我感觉你来自MATLAB世界。无论如何,在python中,
return
返回
None
(更不用说,
是无用的)

您已经正确计算了首字母缩略词-只需返回即可。因此,将最后一个return语句转换为

return acronym
你应该没事的

当然,您可以将整个代码替换为:

def acronyms():
    words = input("Please enter name: ").split()
    return ''.join(w[0].upper() for w in words)

我感觉你来自MATLAB世界。无论如何,在python中,
return
返回
None
(更不用说,
是无用的)

您已经正确计算了首字母缩略词-只需返回即可。因此,将最后一个return语句转换为

return acronym
你应该没事的

当然,您可以将整个代码替换为:

def acronyms():
    words = input("Please enter name: ").split()
    return ''.join(w[0].upper() for w in words)

争论不应该是一个列表吗?你缺少括号

acronym = ''.join([word[0] for word in words.upper().split()])
然后返回首字母缩略词:

return acronym

争论不应该是一个列表吗?你缺少括号

acronym = ''.join([word[0] for word in words.upper().split()])
然后返回首字母缩略词:

return acronym

Python 3.4还是3.1.4?我希望不是后者,因为它已经4-5年了。我建议更新。Python 3.4还是3.1.4?我希望不是后者,因为它已经4-5年了。我建议更新。