美文网首页
8.3返回值

8.3返回值

作者: user_Js | 来源:发表于2020-04-18 12:26 被阅读0次

    返回简单值

    def get_formatted_name(first_name, last_name):
    """返回整洁的姓名"""
    full_name = first_name + " " + last_name
    return full_name.title()

    musician = get_formatted_name('jimi', 'hendrix')
    print(musician)


    image.png

    让实参变成可选

    def get_formatted_name(first_name,middle_name,last_name):
    """返回整洁的姓名"""
    full_name = first_name + ' ' + middle_name + ' ' + last_name
    return full_name.title()

    musician = get_formatted_name('john','lee','hooker')
    print(musician)


    image.png

    def get_formatted_name(first_name,last_name,middle_name=''):
    """返回简洁的姓名"""
    if middle_name:
    full_name = first_name + ' ' + middle_name + ' ' + last_name
    else:
    full_name = first_name + ' ' + last_name
    return full_name.title()

    musician = get_formatted_name('jimi','hendrix')
    print(musician)

    musician = get_formatted_name('john','hooker','lee')
    print(musician)


    image.png

    返回字典

    def build_person(first_name,last_name):
    """返回一个字典,其中包含有关一个人的信息"""
    person = {'first':first_name,'last':last_name}
    return person

    musician = build_person('jimi', 'hendrix')
    print(musician)


    image.png

    能退出的循环

    def get_formatted_name(first_name,last_name):
    """返回简洁的姓名"""
    full_name = first_name + ' ' + last_name
    return full_name.title()

    while True:
    print("\nPlease tell me you name:")
    print("(enter 'q' at any time to quit)")

    f_name = input("first_name: ")
    if f_name == 'q':
        break
        
    l_name = input("last_name: ")
    if l_name == 'q':
        break
        
    formatted_name = get_formatted_name(f_name,l_name)
    print("\nHello, " + formatted_name + "!")
    
    image.png

    相关文章

      网友评论

          本文标题:8.3返回值

          本文链接:https://www.haomeiwen.com/subject/pevyuhtx.html