[Tutor] Having Unusual results
Steven D'Aprano
steve at pearwood.info
Sat May 2 09:56:51 CEST 2015
On Sat, May 02, 2015 at 04:12:40AM +0000, Jag Sherrington wrote:
> 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
code something horrible. Have a look how it ends 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)
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
More information about the Tutor
mailing list