Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的回溯错误_Python_Runtime Error_Traceback - Fatal编程技术网

python中的回溯错误

python中的回溯错误,python,runtime-error,traceback,Python,Runtime Error,Traceback,当我在程序中使用float()方法时,我得到了一个错误。你能帮我一下吗。我使用的是python 3.4.0a4 以下是节目: import urllib.request price = 99.99 while price > 4.74: page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html") text = page.read().decode("utf8") where = text.

当我在程序中使用
float()
方法时,我得到了一个错误。你能帮我一下吗。我使用的是python 3.4.0a4

以下是节目:

import urllib.request

price = 99.99
while price > 4.74:
   page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html")
   text = page.read().decode("utf8")
   where = text.find('>$')
   start_of_price = where + 2
   end_of_price = start_of_price + 4
   price = float(text[start_of_price:end_of_price])
Print("Buy!")
这就是我得到的错误:

Traceback (most recent call last):
  File "F:/Python/python 8.py", line 11, in <module>
    price = float(text[start_of_price:end_of_price])
ValueError: could not convert string to float: '!DOC'
回溯(最近一次呼叫最后一次):
文件“F:/Python/Python 8.py”,第11行,在
价格=浮动(文本[价格的开始:价格的结束])
ValueError:无法将字符串转换为浮点:'!博士

似乎您在错误的位置剪切了网页字符串,而
文本[价格的开始:价格的结束]
的结果是
!文档


这是无效的数字,因此无法转换为浮点。

这是Head first编程中给出的精确代码。链接被破坏了,我得到了纠正它的结果。谢谢你的帮助

您找到了错误的
$
。如果您正在解析HTML,请考虑使用HTML解析器来替代,例如“漂亮的汤”。除非您预先定义了<代码>打印< /代码>,否则您将在最后一行得到<代码>命名空间< /代码>,因为它应该是<代码>打印< /代码>。Python是区分大小写的,记住。你的切片字符串,
text[start\u of_price:end\u of_price]
不是一个数字。在另一个问题上,用浮点数进行计算是不明智的,它们不能通过计算完全准确地存储,因此可能会发生舍入错误。相反,您应该使用整数来保存货币量中的便士数,或十进制模块。