Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How do you run a function without parameters?

Write a function music_func that takes 3 parameters music type, music group, vocalist and prints them all out as shown in the example below.

In case no input is provided by the user, the function should assume these values for the parameters: "Classic Rock", "The Beatles", "Freddie Mercury".

For example:

Input:

Alternative Rock,Pearl Jam,Chris Cornell

Output:

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()
like image 778
Chuepapiii Avatar asked Sep 23 '26 09:09

Chuepapiii


1 Answers

You can utilize default arguments

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

Now calling the function with no arguments will default to using these options

like image 151
Mitch Avatar answered Sep 24 '26 23:09

Mitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!