Learn python the hard way–Ex.18
敲的代码如下:
# this one is like your scripts with argv
def print_two (*args):
arg1, arg2 = args
print(f"arg1: {arg1}, arg2: {arg2}")
# ok.that *args is actually pointless, we can just do this
def print_two_again (arg1, arg2):
print(f"arg1: {arg1}, arg2: {arg2}")
# this just takes one argument
def print_one(arg1):
print(f"arg1: {arg1}")
# this one takes no argements
def print_none():
print("I got nothin'.")
print_two("Zed","Shaw")
print_two_again("Zed","Shaw")
print_one("First!")
print_none()
教训1:
1.def 一定要顶格写,否则在powershell 运行的时候会报错;
2.函数里第一行要空4格(目的是要格式规整?我试过了5678行也可以正常运行)。
Study drills:
1. Did you start your function definition with def?
-Yes
2. Does your function name have only characters and _ (underscore) characters?
-Yes
3. Did you put an open parenthesis right after the function name?
-Yes.
4. Did you put your arguments after the parenthesis separated by commas?
-Yes.
5. Did you make each argument unique (meaning no duplicated names)?
-Yes.
6. Did you put a close parenthesis and a colon after the arguments?
-Yes.
7. Did you indent all lines of code you want in the function four spaces? No more, no less.
-Yes
8. Did you “end” your function by going back to writing with no indent (dedenting, we call it)?
-Yes
When you run (“use” or “call”) a function, check these things:
1. Did you call/use/run this function by typing its name?
-Yes
2. Did you put the ( character after the name to run it?
-Yes
3. Did you put the values you want into the parentheses separated by commas?
-Yes
4. Did you end the function call with a ) character?
-Yes