我写的代码:
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)