小编Sak*_*n55的帖子

为什么.format(function())不起作用,但%打印该值?

我写的代码:

def ball(f):  
    py_ball = f * 5
    u_ball = py_ball / 10  
    return py_ball, u_ball
print("py ball: {}, u ball: {}".format(ball(100)))
Run Code Online (Sandbox Code Playgroud)

当我使用时str.format,它抛出一个异常:

Traceback (most recent call last):
  File "rear.py", line 5, in 
    print("py ball: {}, u ball: {}".format(ball(100)))
IndexError: tuple index out of range

但如果我使用%格式:

print("py ball: %d, u ball: %d" % ball(100))
Run Code Online (Sandbox Code Playgroud)

它工作正常并产生:

py ball: 500, u ball: 50
Run Code Online (Sandbox Code Playgroud)

python string-formatting python-3.x

3
推荐指数
1
解决办法
63
查看次数

标签 统计

python ×1

python-3.x ×1

string-formatting ×1