简体   繁体   English

python中的Traceback错误

[英]Traceback error in python

When I use the float() method in a program , I am getting an error. 当我在程序中使用float()方法时,出现错误。 Can you please help me with that. 你能帮我吗 I am using python 3.4.0a4. 我正在使用python 3.4.0a4。

This is the program: 这是程序:

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!")

and this is the error I get: 这是我得到的错误:

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'

It seems like you sliced the string of the web page at the wrong position, and the result of text[start_of_price:end_of_price] is !DOC . 似乎您将网页的字符串切到了错误的位置,并且text[start_of_price:end_of_price]!DOC

This is not a valid number and can hence not be converted to a float. 这不是有效数字,因此不能转换为浮点数。

This is the exact code given in Head first Programming. 这是在“先头编程”中给出的确切代码。 The link was broken and I got the output on correcting it. 链接已断开,我得到了纠正它的输出。 Thanks for your help.. 谢谢你的帮助..

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

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