Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 While Loop Trouble
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Comp.Sci




PostPosted: Mon Oct 10, 2011 8:39 am   Post subject: 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!
Sponsor
Sponsor
Sponsor
sponsor
Tang




PostPosted: Mon Oct 10, 2011 10:05 am   Post subject: 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




PostPosted: Mon Oct 10, 2011 10:38 am   Post subject: RE:While Loop Trouble

code:
while total > 10000

This will never return true.

code:
break

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


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




PostPosted: Mon Dec 12, 2011 5:21 pm   Post subject: Re: While Loop Trouble

You're code doesn't make sense...
In the beginning you say that:
code:
layer = 0
balls = layer**2
total = 0


Alright, this is good...
Then you start the while statement:
code:
while total > 10 000:

But Total is zero? So total can't be over 10 000, therefore this statement is false. The correct way to put it would be:
code:
white total < 10 000:

Now the loop will run as long as total is less than 10 000.
The break you have in there means that once you enter the while, the code will break out of it, you don't want that. Plus, you already have said that this loop will only run as long as total is less than 10 000, which is all you need.

The below is within while loop I assume...
code:
if total < 10 000:
     layer = layer + 1
     total = balls


No need for the if statement since you already stated the conditions in the while loop statement.

When you re-make the total variable, you're making it equal to balls. Which means, total is the same number as balls. You need to ADD balls to the total... So, change this part to this while kept in the while loop.
code:
layer = layer + 1
total = total + balls (or you can do "total += balls")


Oh... You forgot to re-state the balls variable.
code:
layer = 0
balls = layer**2 # -> therefore balls = 0 (zero squared is zero)


In the loop, you're not changing balls, balls is still zero. This means, total is equal to zero.
This means you need to re-make balls in the loop.

Then finally at the end you're done, and you have to print the layer and then totals. So you're code fixed should look like this:

code:
layers = 0
balls = 0
total = 0

while total < 10000:
     layers = layers + 1
     balls = layers**2 #Re-made the variable balls to re-make the variable...
     total = total + balls

print (layers) # Syntax for latest python is to have everything after the print statement in brackets.
print (total)



I don't have Python installed onto this current computer, but one the above should work...



EDIT Sorry I didn't realize how old this thread was. I'm sorry, remove this post if possible!
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: