[Tutor] New at Python

Alan Gauld alan.gauld at btinternet.com
Mon Oct 6 02:14:23 CEST 2014


On 05/10/14 23:40, Mike Spaulding wrote:
> Given that n refers to a positive int use a while loop to compute the sum of
> the cubes of the first n counting numbers, and associate this value with
> total . Use no variables  other than n , k , and total .

> lab and the accompanying online book.  The biggest problem that I am having
> is trying to figure out how to start off on problems such as the one above.

You need to break the problem into sub problems until you wind up with 
things you can do. For example in the above exercise:

1) can you associate a variable called n with an integer?
2) can you use a while loop to iterate a number of times?
3) can you calculate the cube of a number?
4) can you total two numbers and store the result?
5) can you display the final total?

Assuming you can do each of those bits you need to organize
them into a structure to form your program.

You need to store a value in n and use a while loop to loop
that many times. You need a counter to store the number of
loops. For each time through the loop you need to calculate
the cube of the counter. You then need to add that result
to the running total.

When the loop ends you need to report the total.

Its the same with most beginner exercises, you divide it into
hunks and solve each chunk. Then assemble the bits to solve
the whole.

Once the problems get a lot bigger you need to apply some more
advanced techniques which we call analysis and design. But for
beginner stuff the divide and conquer approach works just fine.

Does that help?
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list