gpt4 book ai didi

Python:如何运行没有参数的函数?

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:33 27 4
gpt4 key购买 nike

编写一个函数 music_func 接受 3 个参数 music type, music group, vocalist 并将它们全部打印出来,如下例所示。

如果用户未提供任何输入,该函数应采用这些参数值:“Classic Rock”、“The Beatles”、“Freddie Mercury”。

例如:

输入:

Alternative Rock,Pearl Jam,Chris Cornell

输出:

The best kind of music is Alternative Rock
The best music group is Pearl Jam
The best lead vocalist is Chris Cornell

Note : The print statements will go inside the music_func(). For example: print("The best kind of music is" + ...)

#definition for music_func goes here
def music_func(a,b,c):
if a is None or b is None or c is None:
print("The best kind of music is Classic Rock")
print("The best music group is The Beatles")
print("The best lead vocalist is Freddie Mercury")
else:
print("The best kind of music is",a)
print("The best music group is",b)
print("The best lead vocalist is",c)
return
def main():
music, group, singer = input().split(',')
music_func(music, group, singer)
music_func() #This is suppose to print the first if-statements
main()

最佳答案

你可以使用默认参数

def music_func(a="Beatles", b="Classic Rock", c="Freddie"):

现在调用不带参数的函数将默认使用这些选项

关于Python:如何运行没有参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48860068/

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