Python format() 函数 KeyError

GTS*_*Joe 1 python

我是 Python 新手,尝试使用 format() 函数时遇到 KeyError:

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are { age } years old".format( age )

sentence1 = hello( 'Mark', 17 )
Run Code Online (Sandbox Code Playgroud)

错误:

KeyError: ' age '
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Bar*_*mar 5

如果在格式字符串中使用命名占位符,则需要提供命名参数。

并且占位符中的名称周围不应有空格。

def hello( name="Sean", age=0 ):
    return 'Hello, ' + name + " you are {age} years old".format(age=age)
Run Code Online (Sandbox Code Playgroud)