P31函数参数
We already learned to create functions which accpet a parameter and return values
We created a function here called get_initial ,When you call get_initial you pass in a name; then we call and our code that's calling get_initial,and it passes on a first_name.Whatever is passed in gets used in the code, wherever you see the word name. and when this function is completed it returns a value. That returned value I call the function I have to have a place to put it.
Fuctions can accept multiple parameters 你可以给函数传入多个参数
def get_initial(name,force_uppercase): if force_uppercase: initial = name[0:1].upper() else: initial = name[0:1] return initial first_name = input('Enter your first_name: ') first_name_initial = get_initial(first_name,False) print('Your initial is: ' + first_name_initial)
Make sure you pass the parameters in the order they're declared.
顺序(first_name,false)
`we can specify a default value for a parameter: looking up documentation for functions, take a look and see if some of the parameters have default values.If they do that means you don't have to pass in a value for them.默认值
def get_initial(name,force_uppercase=True): if force_uppercase: initial = name[0:1].upper() else: initial = name[0:1] return initial first_name = input('Enter your first_name: ') first_name_initial = get_initial(first_name) print('Your initial is: ' + first_name_initial)
`You can also assign the values to parameters by name when you call the function: When I call the function I can say, for that parameter force_uppercase ,I set the value to True. For the parameter callde name, i would like to use the value of first_name. So, I doesn't matter whether I specify the name.By naming the parameters, it doesn't matter what order I specify them in.
def get_initial(name,force_uppercase): if force_uppercase: initial = name[0:1].upper() else: initial = name[0:1] return initial first_name = input('Enter your first_name: ') first_name_initial = get_initial(force_uppercase=True,name=first_name) print('Your initial is: ' + first_name_initial)
Using the named notation when calling functins makes your code more readable: where we had to log errors when things went wrong in our code, you would sometimes see code like this:(示范)One of the coders on our team creates a function that we pass it the error message and the error code , what time the error occurred all these parameters.
def error_logger(error_code, error_severity, log_to_ab, error_message, source_module): print('oh no error: ' + error_message) #Imagine code here that logs our error to a database or file first_number = 10 second_nmuber = 5 if first_number > second_nmuber: error_logger(45,1,True,'second number greater than first','ma_math_method')
改写:
def error_logger(error_code, error_severity, log_to_ab, error_message, source_module): print('oh no error: ' + error_message) #Imagine code here that logs our error to a database or file first_number = 10 second_nmuber = 5 if first_number > second_nmuber: error_logger(error_code = 45,error_severity = 1,log_to_ab = True, error_message='second number greater than first',source_module='ma_math_method')
P32 实 *** 函数参数
如上
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)