简体   繁体   English

为什么在logging.info中显示名称错误?

[英]Why Name error is showing in logging.info?

I am getting error for the below code: 我收到以下代码的错误:

logging.info("Hypotenuse of {a}, {b} is {c}".format(a=3, b=4, c=hypotenuse(a,b))) NameError: name 'a' is not defined 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)))

Desired Out 期望的

INFO:root:Hypotenuse of 3, 4 is 5.0

I'm pretty sure the only way it will work is defining the variables before the call: 我很确定它将起作用的唯一方法是在调用之前定义变量:

a,b = (3,4)
logging.info("Hypotenuse of {a}, {b} is {c}".format(a=a, b=b, c=hypotenuse(a,b))

possibly a little clearer: 可能更清楚一点:

a,b = (3,4)
logging.info("Hypotenuse of {}, {} is {}".format(a, b, hypotenuse(a,b))
logging.info("Hypotenuse of {a}, {b} is {c}".format(a=3,b=4, c=hypotenuse(3,4)))

you can try : 你可以试试 :

a = 3
b = 4
logging.info(f'Hypotenuse of {a}, {b} is {hypotenuse(a, b)}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM