[Tutor] Please help, the program is not behaving as I want

Joel Goldstick joel.goldstick at gmail.com
Tue May 17 18:13:51 CEST 2011


On Tue, May 17, 2011 at 11:20 AM, I. Dooba <idooba at gmail.com> wrote:

> I'm new to programming and have decided to start with Python following the
> advice of experts including Eric S. Raymond.
> So far I've learnt quite a lot from this list.
>
> However, I've the following problem.
>
> The program was designed (as part of my practice with Head First
> Programming) to check whether an ingredient is Halal or Haram.
> I've a simple table online where some ingredients are either halal or
>  haram.
>

You don't have a simple table in this webpage.  You should use 'View Source"
from your browser to actually see what your program is reading.  What you do
have quite far down are words like this between <p> tags:

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Water Halal</p>
<p>Ethanol Halal</p>
<p>Blood Haram</p>
<p>Alcohol Haram</p>
<p>Benzoic Acid Halal</p>



>
> For example, if the user enters "water" the program is supposed to tell him
> that it's halal;
> but it's returning "&gt;" and other characters I don't want to see.
>
> What am I doing wrong?
>
> import urllib.request
>
> this gets access to  the webpage

> page = urllib.request.urlopen("
> http://halalingredients.theparleyonline.com/")
>

this reads the whole page into your text variable

> text = page.read().decode("utf8")
>
>
this lets you type in a word, and uses that word to see if it can find it in
the text.  If it does, it returns the index where it finds the start of the
word

> halal_haram_status = text.find(input("Enter the ingredient:  "))
>

if it can't find a match it will return -1

This is strange.  I believe you want to add the length of the word you input
plus 1 (for the space) to the index you found above.

> begin_final_status = halal_haram_status + 3
>

This is ok since halal and haram are both 5 characters long

> end_final_status = begin_final_status + 5
> status = text[begin_final_status:end_final_status]
> print(status)
>
>
>
Be careful to type the word exactly as printed.  If you the a word that is
not in your list, you might get what looks like nonsense since it will just
look to the 5 characters after the word you give it to find

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> Since I'm on my lunch break I thought I'd dig into this


-- 
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110517/41c548d9/attachment.html>


More information about the Tutor mailing list