[Tutor] Fwd: Re: Having Unusual results

Alan Gauld alan.gauld at btinternet.com
Sat May 2 13:12:39 CEST 2015


CCing the list. Please use Reply ALL to include the list.

On 02/05/15 10:58, Jag Sherrington wrote:
> Hi Alan thank you for your help have done what you sugested and still the results don't add up?
>
>
> # how many hot dogs left
> dogsleft = (packdogs * 10 and - dogs_needed)

You removed the comma. But you added an 'and'.
You need a straight math equation here. By including
'and' you make it a boolean expression and the way
Python evaluates that is to work out the left side
and if it is false(zero) return the value.
if it is true(non zero) then it works out the right
side and return that.

This is done vbecause of the rule in boolean algebra that

TRUE and B = B

So in your case it evaluates it as:

(packdogs * 10 and - dogs_needed)

First it works out packdogs * 10, which is 20, and
therefore True.

It then returns -dogs_needed which is -20 as the result.

That's why you get what you get.

The fix is just to do the math and nothing else, no commas, no 'and'.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos





More information about the Tutor mailing list