[英]Why Name error is showing in logging.info?
我收到以下代碼的錯誤:
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
我很確定它將起作用的唯一方法是在調用之前定義變量:
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))
logging.info("Hypotenuse of {a}, {b} is {c}".format(a=3,b=4, c=hypotenuse(3,4)))
你可以試試 :
a = 3
b = 4
logging.info(f'Hypotenuse of {a}, {b} is {hypotenuse(a, b)}')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.