Try editing the code to make the Output say:
Need a hint?
Look at the words in quotes (aka strings): "..."
1
Hello there!
Great! Now make it say "Hello" to you (your name)
2
Hello Shelly!
The line you just edited "calls" the function printHello, using an argument ("your name").
def printHello(person):
print("Hello " + person + "!")
printHello("Shelly")
To "define" (create) a function, we use the keyword def,
def printHello(person):
print("Hello " + person + "!")
printHello("Shelly")
followed by the name of your function,
def printHello(person):
print("Hello " + person + "!")
printHello("Shelly")
and any arguments it takes, in parentheses ( )
def printHello(person):
print("Hello " + person + "!")
printHello("Shelly")
this line always ends with a colon
def printHello(person):
print("Hello " + person + "!")
printHello("Shelly")
& the indented line(s) after the colon is what the function actually does when it's "called"
Remember: This is how you "call" the function
def printHello(person):
print("Hello " + person + "!")
printHello("Shelly")
Let's change the name of the function, to whatever you want
3
Remember to change it in both places!
def whateverYouWant(person):
print("Hello " + person + "!")
whateverYouWant("Shelly")
Let's change the name of the argument, too
4
Remember to change it in both places!
def whateverYouWant(x):
print("Hello " + x + "!")
whateverYouWant("Shelly")
Now let's change the string that gets printed
5
def whateverYouWant(x):
print("Whatever " + x + " wants")
whateverYouWant("Shelly")
Finally, let's call the function with a different argument:
6
def whateverYouWant(x):
print("Whatever " + x + " wants!")
whateverYouWant("Moses")
Whatever Moses wants!
What if you want to call the function with two arguments?
Whatever ___ and ___ want.
Let's modify the function to take two arguments:
7
def whateverYouWant(person1, person2):
print("Whatever " + person1 + " and " + person2 + " want.")
whateverYouWant("Shelly", "Moses")
Now let's use a shortcut to do the same thing - without all those plus signs!
8
def whateverYouWant(person1, person2):
print("Whatever %s and %s want." %(person1, person2))
whateverYouWant("Shelly", "Moses")
This is called a formatter
What happens when you switch person1 and person2 in the formatter?
9
def whateverYouWant(person1, person2):
print("Whatever %s and %s want." %(person2, person1))
whateverYouWant("Shelly", "Moses")
Then, what happens if you switch person1 and person2 in the argument list?
10
def whateverYouWant(person2, person1):
print("Whatever %s and %s want." %(person2, person1))
whateverYouWant("Shelly", "Moses")
Write a function that takes three words as arguments, and prints a sentence containing all three words.
challenge:
Python Strings
By Michelle Lim
Python Strings
- 2,431