简体   繁体   中英

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


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)}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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