Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Your First Function
A function is a named piece of code you can run whenever you want. You write it once and call it as many times as you like.
def shout(word):
return word.upper() + "!"
shout("hello") # "HELLO!"Three things are happening there:
def shout(word): names the function and says it takes one input, called word.return sends a value back to whoever called the function. A function with no return gives back None.f-strings are the normal way to build a string out of other values. Put an f before the quote and put any expression in { }:
name = "Ada"
f"Hi {name}, you are {20 + 16}" # "Hi Ada, you are 36"Your task:
Write greet(name, age) that returns exactly this sentence:
Hi NAME, you are AGE years old.
So greet("Ada", 36) returns "Hi Ada, you are 36 years old."
Example Tests
greet("Ada", 36) builds the full sentence
Input: {"age":36,"name":"Ada"}
Expected: "Hi Ada, you are 36 years old."
A different name and age produce a different sentence
Input: {"age":45,"name":"Grace"}
Expected: "Hi Grace, you are 45 years old."
def greet(name, age):
"""
Build a greeting sentence.
Args:
name: a string, e.g. "Ada"
age: a whole number, e.g. 36
Returns:
A string like "Hi Ada, you are 36 years old."
"""
# YOUR CODE HERE
passRun your code to see results
⌘↵ runs against the visible tests