Functions that return single values
You're getting very good at this! Try your hand at another modification to the shout() function so that it now returns a single value instead of printing within the function. Recall that the return keyword lets you return values from functions. Parts of the function shout(), which you wrote earlier, are shown. Returning values is generally more desirable than printing them out because, as you saw earlier, a print() call assigned to a variable has type NoneType.
Questo esercizio fa parte del corso
Introduction to Functions in Python
Istruzioni dell'esercizio
- In the function body, concatenate the string in
wordwith'!!!'and assign toshout_word. - Replace the
print()statement with the appropriatereturnstatement. - Call the
shout()function, passing to it the string,'congratulations', and assigning the call to the variable,yell. - To check if
yellcontains the value returned byshout(), print the value ofyell.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
# Define shout with the parameter, word
def shout(word):
"""Return a string with three exclamation marks"""
# Concatenate the strings: shout_word
# Replace print with return
print(shout_word)
# Pass 'congratulations' to shout: yell
# Print yell