Discussion:
[Tutor] Having Unusual results
Jag Sherrington
2015-05-02 04:12:40 UTC
Permalink
Hi Can anyone tell me where I am going wrong the end result doesn't add up?
people = int(input('Enter how many people '))numdogs = int(input('Enter how many dogs '))
hotdogs = 10buns = 8dogsleft = 0bunsleft = 0
# total number of hot dogs neededdogs_needed = (numdogs * people)
# how many packages of hot dogspackdogs = (dogs_needed / hotdogs)
# how many packs of buns neededpackbuns = (dogs_needed / buns)
# how many hot dogs leftdogsleft = (packdogs * 10, - dogs_needed)
# how many buns leftbunsleft = (packbuns * 8, - dogs_needed)
print('Here are the details:')
print('Total hot dogs needed: ', dogs_needed)
print('Packs of hot dogs ', packdogs)
print('Packs of buns ', packbuns)
print('Dogs left: ', dogsleft)
print('Buns left: ', bunsleft)
THIS IS THE RESULT
Enter how many people 10Enter how many dogs 2Here are the details:Total hot dogs needed:  20Packs of hot dogs  2.0Packs of buns  2.5Dogs left:  (2.0, -20)Buns left:  (2.5, -20)
Many thanks for your help.
BraveArt Multimedia 

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.p
Ben Finney
2015-05-02 07:46:23 UTC
Permalink
Post by Jag Sherrington
Hi Can anyone tell me where I am going wrong the end result doesn't add up?
Maybe. first, though, you'll need to turn off any “rich text” or other
fancy mangling of your message content. It makes the text
undecipherable.

Just compose your message as plain text, and put the program text and
output in the message.
--
\ “Laugh and the world laughs with you; snore and you sleep |
`\ alone.” —anonymous |
_o__) |
Ben Finney

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription op
Alan Gauld
2015-05-02 07:52:51 UTC
Permalink
Post by Jag Sherrington
Hi Can anyone tell me where I am going wrong the end result doesn't add up?
I assume you are posting in HTML, your code is very hard to read with
missing newlines. Please check your settings and post in plain text.

I'm not sure what you mean by "doesn't add up"?
What were you expecting to be different?
Is it the 2.0 and 2.5 in the last two results?
Post by Jag Sherrington
people = int(input('Enter how many people '))numdogs = int(input('Enter how many dogs '))
hotdogs = 10buns = 8dogsleft = 0bunsleft = 0
# total number of hot dogs neededdogs_needed = (numdogs * people)
# how many packages of hot dogspackdogs = (dogs_needed / hotdogs)
# how many packs of buns neededpackbuns = (dogs_needed / buns)
# how many hot dogs leftdogsleft = (packdogs * 10, - dogs_needed)
One thing that is odd. Why did you separate this calculation
with the comma? Were you intending completing it later on?
Post by Jag Sherrington
# how many buns leftbunsleft = (packbuns * 8, - dogs_needed)
same here
Post by Jag Sherrington
print('Here are the details:')
print('Total hot dogs needed: ', dogs_needed)
print('Packs of hot dogs ', packdogs)
print('Packs of buns ', packbuns)
print('Dogs left: ', dogsleft)
print('Buns left: ', bunsleft)
THIS IS THE RESULT
Enter how many people 10
Enter how many dogs 2
Here are the details:
Total hot dogs needed: 20
Packs of hot dogs 2.0
Packs of buns 2.5
Dogs left: (2.0, -20)
Buns left: (2.5, -20)

Assuming its the 2.0 and 2.5 that you are querying, I confess I'm
confused too. I don't get that when I run your code (after fixing
the newlines):

Enter how many people 10
Enter how many dogs 2
Here are the details:
Total hot dogs needed: 20
Packs of hot dogs 2.0
Packs of buns 2.5
Dogs left: (20.0, -20)
Buns left: (20.0, -20)

Which is exactly what I'd expect.
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-05-02 07:56:51 UTC
Permalink
Post by Jag Sherrington
Hi Can anyone tell me where I am going wrong the end result doesn't add up?
Hi Jag, it's hard to say exactly what's going wrong because you're
posting with "Rich Text" that ends up mangling the formatting of your
Post by Jag Sherrington
people = int(input('Enter how many people '))numdogs = int(input('Enter how many dogs '))
hotdogs = 10buns = 8dogsleft = 0bunsleft = 0
# total number of hot dogs neededdogs_needed = (numdogs * people)
# how many packages of hot dogspackdogs = (dogs_needed / hotdogs)
# how many packs of buns neededpackbuns = (dogs_needed / buns)
# how many hot dogs leftdogsleft = (packdogs * 10, - dogs_needed)
# how many buns leftbunsleft = (packbuns * 8, - dogs_needed)
etc. So we have to try to reconstruct what the code is supposed to be,
rather than what we are given.

If you are planning to stay here for a while (and we would be really
happy for you to do so!) you should spend some time fixing the email,
otherwise (1) you're going to have a bad time, and (2) we're probably
going to just give up trying to help. (Sorry, but we have only so much
time and energy we're able to give.)

But for now, let me see what I can do... my guess is that you are trying
to calculate the number of packets of hot dogs and buns needed to feed
some people.

hotdogs = 10 # hot dogs per packet
buns = 8 # buns per packet
dogsleft = bunsleft = 0 # how many left over

dogs_needed = numdogs * people # Each person gets numdogs hot dogs
packdogs = dogs_needed / hotdogs
packbuns = dogs_needed / buns

Your code seems to be okay up to this point. But the next two lines seem
to be wrong:

dogsleft = (packdogs * 10, - dogs_needed)
bunsleft = (packbuns * 8, - dogs_needed)

The commas turn them into tuples. A tuple is a collection of multiple
values. If you remember your high school maths, tuples are like X-Y
coordinate pairs, except they can hold anything, not just numbers, and
not just a pair of them. But the important thing is that

packdogs * 10, - dogs_needed


gives you a pair of numbers:

packdogs * 10

and

-dogs_needed


which looks like this when printed:

(2.0, -20)

So you need to change those two lines to remove the commas:

dogsleft = (packdogs * 10 - dogs_needed)
bunsleft = (packbuns * 8 - dogs_needed)

Does that fix the program? If not, what does it do, and what did you
expect it to do instead?
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-05-02 08:36:19 UTC
Permalink
Post by Jag Sherrington
Hi Can anyone tell me where I am going wrong the end result doesn't add up?
Leaving out the Python side for a moment let's look again at one part of
your problem:

You need 20 buns.
There are 8 buns in a package.
How many packages do you need?

You could say 2.5 because 2.5 * 8 == 20. But when you go to the shop you are
limited to buying complete packages. Two won't do, so you have to take three
packages and cope with the 4 extra buns.
Post by Jag Sherrington
20 // 8
2

Integer division always gives you a number equal or one below the actual
Post by Jag Sherrington
21 // 8
2
Post by Jag Sherrington
23 // 8
2
Post by Jag Sherrington
24 // 8
3

To find out if you need an extra package you can calculate the rest with
Post by Jag Sherrington
buns = 20
package_size = 8
buns - (buns // package_size) * package_size
4
Post by Jag Sherrington
buns = 20
package_size = 8
whole_packages = buns // package_size
missing_buns = buns - (buns // package_size) * package_size
... total_packages = whole_packages + 1
... else:
... total_packages = whole_packages
...
Post by Jag Sherrington
total_packages
3

Can we simplify that? Python has an operator to calculate the rest or
Post by Jag Sherrington
20 % 8
4
Post by Jag Sherrington
21 % 8
5
Post by Jag Sherrington
22 % 8
6
Post by Jag Sherrington
23 % 8
7
Post by Jag Sherrington
24 % 8
0
Post by Jag Sherrington
divmod(20, 8)
(2, 4)

With that the calculation becomes
Post by Jag Sherrington
buns = 20
package_size = 8
whole_packages, missing_buns = divmod(buns, package_size)
total_packages = whole_packages
if missing_buns: total_packages += 1
...
Post by Jag Sherrington
total_packages
3
Post by Jag Sherrington
unused_buns = (total_packages * package_size) % buns
unused_buns
4

Enjoy your meal ;)

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Dave Angel
2015-05-02 10:54:57 UTC
Permalink
Post by Peter Otten
With that the calculation becomes
buns = 20
package_size = 8
whole_packages, missing_buns = divmod(buns, package_size)
total_packages = whole_packages
if missing_buns: total_packages += 1
...
total_packages
3
And that can be simplified:

buns = 20
package_size = 8
total_packages = (buns + package_size - 1) // package_size

#desired answer 3


Or, to take better advantage of the Python library:

import math
total_packages = math.ceil(buns/package_size)

This is exactly what the ceiling and floor mathematical concepts are
needed for.

Note, I'm using the fact that the OP is writing in Python 3. If not,
one should probably add
from __future__ import division
.
--
DaveA
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Jag Sherrington
2015-05-06 07:36:17 UTC
Permalink
Excellent all working good, thank you.
Regards, Jag BraveArt Multimedia
Post by Peter Otten
With that the calculation becomes
buns = 20
package_size = 8
whole_packages, missing_buns = divmod(buns, package_size)
total_packages = whole_packages
if missing_buns: total_packages += 1
...
total_packages
3
And that can be simplified:

buns = 20
package_size = 8
total_packages = (buns + package_size - 1) // package_size

#desired answer 3


Or, to take better advantage of the Python library:

import math
total_packages = math.ceil(buns/package_size)

This is exactly what the ceiling and floor mathematical concepts are
needed for.

Note, I'm using the fact that the OP is writing in Python 3.  If not,
one should probably add
    from __future__ import division
.
--
DaveA
_______________________________________________
Tutor maillist  -  ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tut
Alan Gauld
2015-05-02 11:12:39 UTC
Permalink
CCing the list. Please use Reply ALL to include the list.
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



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...