【发布时间】:2018-07-18 07:44:40
【问题描述】:
import time
def taym():
time.sleep(5)
def getprice():
myfile = open('C:\\Users\\DELL\\Desktop\\Python\\html1.html')
txt = myfile.read()
t = txt.find("$")
it = float(txt[t-4:t])
it=8
while it != 1000:
getprice()
if it <= 4.74:
print("Price is Ok!")
taym()
else:
print("The price of the coffee beans is "+txt[t-4:t+1])
taym()
当我在 python3 中运行此代码时,我收到如下错误消息:
"print("The price of the coffee beans is "+txt[t-4:t+1])
NameError: name 'txt' is not defined."
我知道我可以在循环中使用来自 getprice() 的原始代码,但我需要知道为什么当我调用一个函数时它不起作用。
【问题讨论】:
-
您的函数
getprice()似乎更改了一个使用变量:txt, t, it,但这些是该函数的本地变量。您的主循环使用具有这些名称的变量,但它们不是相同的变量。也许您打算退回它们。
标签: python function file variables text-files