带有关键字args的python函数问题

时间:2019-04-25 11:11:38

标签: python function

我无法打印此函数,应该使用带有可选关键字args的函数来制作句子。

I Love Chicago!
Python is great!!!!
I guess Java is okay. I guess Java is okay.
LET'S GO CUBS!!

这是我的代码,什么也没打印

def speak(message, capitalize_message, num_times_repeat_message, num_exclamation_points=1):

    speak("I love Chicago", 1, 1, False)
    speak("Python is great", 4, 1, False)
    speak("I guess Java is okay", 0, 2, False)
    speak("LET's GO CUBS", 2, 1, True)

print(speak)
print(speak)
print(speak)
print(speak)

2 个答案:

答案 0 :(得分:1)

我认为您正在寻找这个吗?:

    def speak(message, capitals=True, num_repeats=1, num_exclame=1):
         if capitals:
             message = message.capitalize() # capitalize if needed
         message += '!' * num_exclame # add exclamation marks
         return message * num_repeats 

然后调用如下函数:

print(speak('python is great', capitals=True, num_repeats=2, num_exclame=2))

或者说出来打印出来:

    def speak(message, capitals=True, num_repeats=1, num_exclame=1):
         if capitals:
             message = message.capitalize()
         message += '!' * num_exclame
         print(message * num_repeats)

简单地:

speak('python is great', True, 2, 3)

答案 1 :(得分:0)

您在函数中什么也不做,以下是如何编写函数的提示:

def speak(message, capitalize_message, num_times_repeat_message, num_exclamation_points=1):
    print message
    #write your logic here based on parameters

#Below lines calls the speak function
speak("I love Chicago", 1, 1, False)
speak("Python is great", 4, 1, False)
speak("I guess Java is okay", 0, 2, False)
speak("LET's GO CUBS", 2, 1, True)