以下代码出现错误:
logging.info(“ {a},{b}的斜边为{c}”。.format(a = 3,b = 4,c =斜边(a,b))) NameError:名称“ a”未定义
import logging
logging.basicConfig(level=logging.INFO)
def hypotenuse(a, b):
"""Compute the hypotenuse"""
return (a**2 + b**2)**0.5
logging.info("Hypotenuse of {a}, {b} is {c}".format(a=3, b=4, c=hypotenuse(a,b)))
所需
INFO:root:Hypotenuse of 3, 4 is 5.0
答案 0 :(得分:3)
我很确定它将起作用的唯一方法是在调用之前定义变量:
a,b = (3,4)
logging.info("Hypotenuse of {a}, {b} is {c}".format(a=a, b=b, c=hypotenuse(a,b))
可能更清晰一些:
a,b = (3,4)
logging.info("Hypotenuse of {}, {} is {}".format(a, b, hypotenuse(a,b))
答案 1 :(得分:2)
logging.info("Hypotenuse of {a}, {b} is {c}".format(a=3,b=4, c=hypotenuse(3,4)))
答案 2 :(得分:1)
您可以尝试:
a = 3
b = 4
logging.info(f'Hypotenuse of {a}, {b} is {hypotenuse(a, b)}')