python

python(15):函数(2)

2022-04-08  本文已影响0人  Z_bioinfo

1.返回值

函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值。函数返回的值被称为返回值。在函数中,可
使用return语句将值返回到调用函数的代码行

1.返回一个简单值
def get_formstted_name(first_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + ' ' + last_name
    return full_name

musician = get_formstted_name('da ming', 'zhang')#将值存储在变量musician中
print(musician)
da ming zhang
2.让实参变成可选的

有时候,需要让实参变成可选的,这样使用函数的人就只需要在必要时才提供额外的信息,可以使用默认值来让实参变成可选的。
例如,假设要拓展函数get_formstted_name(),使其还处理中间名,为此,可将其修改类似于下面这样

def get_formstted_name(first_name, middle_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + ' ' + middle_name +  ' ' + last_name
    return full_name
musician = get_formstted_name('da ming', 'li',  'zhang')#同时提供名,中间名,姓,这个函数就能正确运行。
print(musician)
da ming li zhang
#但是,并非所有的人都有中间名,如果只提供了了名和姓,函数将不能运行,为了让中间名变成可选的,可给实参 middle_name指定一个默认值:空字符串,并在用户没有提供中间名时不使用这个实参
def get_formstted_name(first_name, last_name,  middle_name=' '):
    """返回整洁的姓名"""
    if middle_name:#如果函数中调用了中间名,if middle_name将为true,
        full_name = first_name + ' ' + middle_name +  ' ' + last_name
    else:#如果没有提供中间名,if middle_name将为false,执行else语块
        full_name = first_name + ' ' + last_name
    return full_name

musician = get_formstted_name('da ming', 'li',  'zhang')
print(musician)
musician = get_formstted_name('da ming',  'zhang')
print(musician)
da ming zhang li
da ming   zhang
3.返回字典
#函数可返回任何类型的值,包括列表和字典
def build_person(first_name, last_name):
    """返回一个字典,其中包含有关一个人的信息"""
    person = {'first': first_name, 'last':last_name}
    return person
musician = build_person('da ming',  'zhang')
print(musician)
{'first': 'da ming', 'last': 'zhang'}
#还可以存储年龄
def build_person(first_name, last_name, age=''):
    """返回一个字典,其中包含有关一个人的信息"""
    person = {'first': first_name, 'last':last_name}
    if age:
        person['age'] = age
    return person
musician = build_person('da ming',  'zhang',age=27)
print(musician)
{'first': 'da ming', 'last': 'zhang', 'age': 27}
4.结合使用函数和while循环
def get_formstted_name(first_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + ' ' + last_name
    return full_name
#这是一个无限循环
while True:
    print('\nplease tell me your name:')
    f_name = input('first name :')
    l_name = input('last name :')
    formstted_name = get_formstted_name(f_name, l_name)
    print('\nhello, ' + formstted_name + '.')
please tell me your name:
first name :da ming
last name :zhang

hello, da ming zhang.
==========================================
def get_formstted_name(first_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + ' ' + last_name
    return full_name
#定义退出条件,退出无限循环
while True:
    print('\nplease tell me your name:')
    print("(enter 'q' at any time to quit)")#当输入q时,退出循环
    f_name = input('first name :')
    if f_name == 'q':
        break
    l_name = input('last name :')
    if l_name == 'q':
        break
    formstted_name = get_formstted_name(f_name, l_name)
    print('\nhello, ' + formstted_name + '.')
please tell me your name:
(enter 'q' at any time to quit)
first name :da ming
last name :zhang

hello, da ming zhang.

please tell me your name:
(enter 'q' at any time to quit)
first name :ni ni
last name :zhao

hello, ni ni zhao.

please tell me your name:
(enter 'q' at any time to quit)
first name :q
上一篇下一篇

猜你喜欢

热点阅读