
-----------------------------------
Comp.Sci
Mon Oct 10, 2011 8:39 am

While Loop Trouble
-----------------------------------
Hi, need some help with this question:

From a collection of 10,000 cannonballs, a square based pyramid is built with a single cannonball on top and a square number on each layer. How many layers can be made? How many cannonballs are left over?

so, i've drawn this out and made a table... What i found out was that the number of cannon balls (for that layer) is equal to the layer squared...
ex. the first layer, 1**2 = 1 cannon ball, the total is 1
the second layer 2**2 = 4 cannon balls, the total is 5

Here is my code:
layer=0
balls=layer**2
total=0

while total > 10000:
    break
if total < 10000:
    layer=layer+1
    total=balls

print layer
print total

My problem here is that i do not know how to get the remainer... and the answer that this code is giving me is 1 for the layer, and 0 for the total...PLEASE HELP!

-----------------------------------
Tang
Mon Oct 10, 2011 10:05 am

RE:While Loop Trouble
-----------------------------------
Well, I have a question first- what is the break supposed to do in this case?

It looks to me like whether or not total>10000 is True, nothing will run in the While loop anyways.

Is the If statement nested inside the while loop?

-----------------------------------
Insectoid
Mon Oct 10, 2011 10:38 am

RE:While Loop Trouble
-----------------------------------
[code]while total > 10000[/code]
This will never return true.

[code]break[/code]
If for some reason the loop actually executes, it won't do anything because the first command in the loop is to quit the loop. 


[code]layer=0 
balls=layer**2 [/code]

What is balls, after this executes? Part of this could be put in the loop though...

Also put your code in [ code] tags to preserve indentation, which is important in Python.

tl;dr your program is completely broken and you need to start over.

-----------------------------------
Linkxgl
Mon Dec 12, 2011 5:21 pm

Re: While Loop Trouble
-----------------------------------
You're code doesn't make sense...
In the beginning you say that:
EDIT Sorry I didn't realize how old this thread was. I'm sorry, remove this post if possible!
