Jump to content


Photo

Problems With Beans-r-us Site


  • Please log in to reply
31 replies to this topic

#21 Elizabeth Robbins

Elizabeth Robbins

    New Member

  • Members
  • Pip
  • 3 posts

Posted 28 December 2013 - 09:45 AM

Did you ever get an answer to this? I am getting the same error!

In Chapter 2, in the program where the float() method is introduced , I am getting an error. Can you please help me with that. I am using 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-u...iz/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'



#22 muzima

muzima

    New Member

  • Members
  • Pip
  • 8 posts
  • Gender:Male
  • Location:Moscow,Russia
  • Interests:Snowboarding, classical music, Greek language. I'd like to learn how to create apps for my Android. Aim - creating app for my Greek dictionary by my own.

Posted 29 December 2013 - 10:43 AM

Did you ever get an answer to this? I am getting the same error!


Try another url

page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.html")


#23 Robert Zero

Robert Zero

    New Member

  • Members
  • Pip
  • 5 posts

Posted 28 June 2014 - 11:54 AM

Hi guys!

I have no internet access in my home at the moment, so I used a local HTML webpage which I manually update the prices in it so I can test whether the script works or not.

The thing is, it works but with a problem I can't seem to be able to solve (or understand):

#import the lib
import urllib.request

# defines initial price
price = 99.99

# iterate the loop while price is greater than 4.74
while price > 4.74:

    # opens the page
    page = urllib.request.urlopen("file:///C:/beanrus/prices.html")

    # reads the page content decoding it in UTF-8
    text = page.read().decode("utf8")

    # Variables

    # substring's index
    where = text.find(">$")

    start_of_price = where + 2
    end_of_price   = start_of_price + 4

    # string containing the price

    price = float(text[start_of_price:end_of_price])

# Buy!
print("Buy it!")


Whenever I change the price in the HTML file and hit the save button in the text editor, at the same time (instantly) I get this error in the Python Shell:

        Traceback (most recent call last):
        File "Z:\Desktop\#1.3 - Pre├žo Baixo.py", line 26, in <module>
        price = float(text[start_of_price:end_of_price])
        ValueError: could not convert string to float: 

I've also used the coded provided in the Head First labs for this book, same problem.

Can anybody help me figure out what the problem is?

Thanks in advance!

P.S.: This is the code in my HTML file:


<html><head><title>Welcome to the Beans'R'Us Pricing Page</title>
<link rel="stylesheet" type="text/css" href="beansrus.css" />
</head><body>
<h2>Welcome to the Beans'R'Us Pricing Page</h2>
<p>Current price of coffee beans = <strong>$5.28</strong></p>
<p>Price valid for 15 minutes from Sun Jun 22 00:19:53 2014.</p>
</body></html>


Edited by Robert Zero, 28 June 2014 - 11:55 AM.


#24 Robert Zero

Robert Zero

    New Member

  • Members
  • Pip
  • 5 posts

Posted 29 June 2014 - 07:30 PM

Just bumping.

C'mon, guys, I'm sure many people are having the same problems (...or not, but hey, I am!)
So please, help me out here!

:D

#25 Robert Zero

Robert Zero

    New Member

  • Members
  • Pip
  • 5 posts

Posted 02 July 2014 - 06:56 PM

Really, people? Is this book/thread forsaken like this?

I'll keep bumping.

#26 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 04 July 2014 - 02:57 AM


@Robert Zero,

Sorry for the late reply. :rolleyes:

I have copy pasted and checked your code with the html file.

The results are fine even if I am changing the price in the "prices.html" file while the program is running.

As per the errors shown by Python, I think that you have mistakenly deleted the ">" or "$" or both from the "prices.html" file so that python is not able to find the string ">$" in the html file.

So, the code "text.find(">$") is returnig the value "-1" to the variable "where" and the error propagates further. The variable "start_of_price" becomes "1" and "end_of_price" becomes "5". But, the text[1:5] are letters not the price (i.e., numbers). So, python is giving you the ValueError that string cannot be converted to a float because only numbers (in string format) can be converted to float or int.

So, open the "prices.html" file in a text editor; check and correct it.

Always be watchful over such errors; the code will silently run but won't give you the desired results. When in such unexpected error situations, try to search about the errors on Google (most possibly you will find a similar error situation).
Also print the different variables you are using in the code to understand where and what is the problem


The forum or thread is not forsaken or abandoned; at least I am alive here.

B) :)

Edited by #TM#, 04 July 2014 - 03:15 AM.


#27 Robert Zero

Robert Zero

    New Member

  • Members
  • Pip
  • 5 posts

Posted 06 July 2014 - 03:13 PM

Thanks for the reply! (...and for being alive too! :D)

Let's see...

I have copy pasted and checked your code with the html file.
The results are fine even if I am changing the price in the "prices.html" file while the program is running.

Sometimes it takes a few "clicks" in the save button to make it happen.

To simulate what I am doing here, you have to:

  • Keep the prices.html file open in one text editor (I'm using Notepad++ at the moment);
  • Run the program with the price in the HTML page previously set higher than the one required to exit the loop;
  • While the program is running, edit the price (not removing anything else) to an even greater value (sometimes the error happens here, sometimes it will keep iterating the loop);
  • Then change it again to a value lesser than the one set in the Python code.

Repeat this process a couple of times and see what happens.

As per the errors shown by Python, I think that you have mistakenly deleted the ">" or "$" or both from the "prices.html" file so that python is not able to find the string ">$" in the html file.


Thanks for the warning but I am completely sure I haven't deleted anything in the HTML code.
If that was the problem, I could sleep with ease tonight. :)

I've even used the original prices.html that can be downloaded in the "Head First Programming"'s book download page.

So, the code "text.find(">$") is returnig the value "-1" to the variable "where" and the error propagates further. The variable "start_of_price" becomes "1" and "end_of_price" becomes "5". But, the text[1:5] are letters not the price (i.e., numbers). So, python is giving you the ValueError that string cannot be converted to a float because only numbers (in string format) can be converted to float or int.

So, open the "prices.html" file in a text editor; check and correct it.


As I said above, no changes've been made to the file "prices.html" except changing the price a couple of times.
In no moment I deleted the ">$" characters thus invalidating this possibility.

I tried to go through the Python's debugger, but the error doesn't happen there. I don't know why.

You said it would give me the text[1:5], what would be "<html" and sure, this can't be converted to float.
But in the error message it would be shown, like this: ValueError: could not convert string to float: '<html', and that does not happen.

Again, nothing's been made to the HTML file except changing the price only so, there's nothing to be fixed.

Always be watchful over such errors; the code will silently run but won't give you the desired results. When in such unexpected error situations, try to search about the errors on Google (most possibly you will find a similar error situation).
Also print the different variables you are using in the code to understand where and what is the problem


I did, a LOT.

Searching, printing variables and etc...

I somehow got hooked up to Visual Studio's debugger. It's so neat it's really hard to let go of it and use another debugger tool.

When running the debugger along with this Python code, the error is not present. That's why I've found this particularly problem odd.

My problem is not with the ValueError. I just can't figure out what's causing it.

I'm really grateful for your reply.
Up to now it seems you've been the only one to show some interest in this problem.

I don't know how this is affecting other people using this book as reference but, it's real and it's happening to me.

If you guys want, I could screencast it in real-time and upload it to my channel on YouTube.

Well, thanks again!

#28 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 07 July 2014 - 01:11 AM


@ Robert Zero, First of all, Thanks for bringing this to notice.
Your debugging skills are great ! :)

Oops ! I didn't notice the error properly.

You are right, it is giving errors like
ValueError: could not convert string to float:
without information about the proper location of the error.

Text editors in linux like Gedit and in windows Notepad (NOT Notepad ++) gives the correct results even changing the price while the program is in running state.
For now you can use Notepad. :)

Wordpad is not allowing to save the file after changing the price. It is saying " The document is open in another application and cannot be accessed".
Using Notepad ++, the file can be edited, but got the error in python console.

Seems to be runtime error. The edited data is not able to be communicated to the python process. But how notepad and text editors in linux like gedit are able to do it ; I don't know. Possibly they are able to create multiple temporary copies of the text file in memory on each edit and save.

While editor "geany" is giving the error "the temporary file used in the memory of computer is getting renamed" by saving it at runtime ( try this by yourself on geany editor).

It seems to the problem of the text editors. B)

Anyway, I am copy pasting an edited Python code to just escape the scenario. (escapist method only; no true reason can be inferred :P )

#import the lib
import urllib.request

# defines initial price
price = 99.99

# iterate the loop while price is greater than 4.74
try:
    while price > 4.74:
        # opens the page
        page = urllib.request.urlopen("file:///C:/Users/TM/Desktop/prices.html")
        # reads the page content decoding it in UTF-8
        text = page.read().decode("utf8")

        # Variables

        # substring's index

        where = text.find(">$")
        start_of_price = where + 2
        end_of_price   = start_of_price + 4

        # string containing the price

        price = float(text[start_of_price:end_of_price])
except ValueError: #Though it is not a value error rather runtime error. :-P
    print ("Oops !!!, try another text editor to edit the html file.")
# Buy!
print("Buy it!")


Edited by #TM#, 07 July 2014 - 02:59 AM.


#29 Robert Zero

Robert Zero

    New Member

  • Members
  • Pip
  • 5 posts

Posted 20 July 2014 - 01:19 PM

@ Robert Zero, First of all, Thanks for bringing this to notice.
Your debugging skills are great ! :)


#TM#, you are a genius! :D

Flawless answer.

I think I would've never mistrusted my beloved text editor (Notepad++)!

I edited the html file using the old, regular Windows' Notepad while the program was running and it worked like a charm!

I used Geany for a couple of weeks on Ubuntu and I had also tried to use Sublime Text 2, on Ubuntu and Linux.

Both show some message saying the same thing that the file is being used by another process/program and cannot be modified.

I think the answer lies in the way notepad (or gedit...) uses to open the file.

I've always had the impression that there was some sort of "delay" in the process of modifying the file while the code was running, thus causing the ValueError to show nothing after the "could not convert string to float: ".

But I can't confirm that.

Well, BIG thanks to you!

Now I can keep it up!

Edited by Robert Zero, 20 July 2014 - 01:22 PM.


#30 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 22 July 2014 - 01:00 AM

#TM#, you are a genius! :D

Flawless answer.

Thank you so much :)

I think I would've never mistrusted my beloved text editor (Notepad++)!

I edited the html file using the old, regular Windows' Notepad while the program was running and it worked like a charm!


Never trust Softwares; not even your code. Trust yourself. And above all, never love softwares or machines. Who knows when softwares become AGENT SMITHs or machines become TERMINATORs. :blink: :P

Anyway, keep up programming, the softwares and their problems come and go with the wind !

Edited by #TM#, 22 July 2014 - 01:03 AM.


#31 TylerMDGL

TylerMDGL

    New Member

  • Members
  • Pip
  • 1 posts

Posted 30 October 2014 - 11:59 AM

I just started reading Head first programming, And like everybody else, I am having issues with the Beans-r-us website. I know the book is for python 3.0, and since i'm using python 3.4.2, Could that be an issue? Also, I've tried to run the same code through python 3.0 and i get the same errors. I've also tried the different variations of the website address and its still not working. I've also tried to turn off my norton security firewall. This is the code:

import urllib.request
page = urllib.request.urlopen("http://www.beans.itc...s-loyalty.html")
text = page.read().decode("utf8")

print(text)




This is my response:
Traceback (most recent call last):
File "C:\Python34\lib\urllib\request.py", line 1174, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Python34\lib\http\client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "C:\Python34\lib\http\client.py", line 1128, in _send_request
self.endheaders(body)
File "C:\Python34\lib\http\client.py", line 1086, in endheaders
self._send_output(message_body)
File "C:\Python34\lib\http\client.py", line 924, in _send_output
self.send(msg)
File "C:\Python34\lib\http\client.py", line 859, in send
self.connect()
File "C:\Python34\lib\http\client.py", line 836, in connect
self.timeout, self.source_address)
File "C:\Python34\lib\socket.py", line 509, in create_connection
raise err
File "C:\Python34\lib\socket.py", line 500, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Tyler92\Desktop\Starbuzz.py", line 2, in <module>
page = urllib.request.urlopen("http://www.beans.itc...s-loyalty.html")
File "C:\Python34\lib\urllib\request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "C:\Python34\lib\urllib\request.py", line 461, in open
response = meth(req, response)
File "C:\Python34\lib\urllib\request.py", line 571, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python34\lib\urllib\request.py", line 493, in error
result = self._call_chain(*args)
File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 676, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Python34\lib\urllib\request.py", line 455, in open
response = self._open(req, data)
File "C:\Python34\lib\urllib\request.py", line 473, in _open
'_open', req)
File "C:\Python34\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "C:\Python34\lib\urllib\request.py", line 1202, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python34\lib\urllib\request.py", line 1176, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10061] No connection could be made because the target machine actively refused it>
>>>

Can Someone please help me?

Thanks!

#32 #TM#

#TM#

    Active Member

  • Members
  • PipPipPip
  • 91 posts
  • Gender:Not Telling

Posted 01 November 2014 - 12:53 AM

page = urllib.request.urlopen("http://www.beans.itcarlow.ie/prices-loyalty.html")

In this code, DONOT use those www, i.e., use http://beans.itcarlow.ie/prices-loyalty.html rather than http://www.beans.itcarlow.ie/prices-loyalty.html
:) B)



And please use
<> button on the editor for copy pasting code and tracebacks
or

[code]
Your code here
[/code]

tag.

Edited by #TM#, 01 November 2014 - 12:57 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users