[prog] Re: [Courses] need a little help with extremly basic python programming
Elizabeth Barham
lizzy at soggytrousers.net
Thu Dec 5 09:39:55 EST 2002
Mary writes:
> I've Cc-ed the programming list, since this post isn't part of a course
> it would probably be better off there.
>
> On Thu, Dec 05, 2002, Guru - wrote:
> > "Exercise 1
> > Write a program that continually reads in numbers from the user and adds
> > them together until the sum reaches 100. Write another program that reads
> > 100 numbers from the user and prints out the sum. "
> > I know that it's to do with loops but I wasn't sure how to do it...
What do you need to know in order to complete task one:
Write a program that continually reads in numbers from the user
and adds them together until the sum reaches 100.
By this I mean, what variables are necessary to accomplish the task?
1) A number that is read in from the user (input_num)
2) A number that accumulates a running total (accumulator)
The only other datum needed is the number 100, which is a constant, and
is not a variable because it does not vary:
Write a program that continually reads in numbers from the user
[input_num] and adds them together until the sum [accumulator]
reaches 100.
Next, we define the condition in which we loop. In this case, it is
simple: ( accumulator < 100 ). There also must need be an
initialization step if we use a variable prior to an assignment (x =
y), as prior to the assignment it is undefined and could contain
anything.
Steps:
1) Define Variables
2) Initialization
3) Loop
There are two, general types of loops, the "check condition before"
and the "check condition after" and in C (I do not know python) these
can be respectively translated as:
while(CONDITION IS TRUE) {
ACTIONs
}
and
do {
ACTIONs
} while(CONDITION IS TRUE);
For the first assignment,
Write a program that continually reads in numbers from the user
[input_num] and adds them together until the sum [accumulator]
reaches 100.
note that there needs to be an action prior to the condition being
checked, so I would suggest the latter (check condition after) loop.
Putting them all together:
Define_Variables:
int input_num;
int accumulator;
Initialization:
accumulator = 0;
Loop:
do {
std::cout << "Please enter a number: ";
std::cin >> input_num ; // place user's number in input_num
accumlator = accumlator + input_num;
}
Loops_Condition:
while(accumulator < 100);
The other while loop would work as well because of the initialization:
int input_num;
int accumulator;
accumulator = 0;
while(accumulator < 100) {
// etc.
}
But this wastes a test (the "if(accumulator < 100)" implied by
"while(accumulator < 100)") because we know that accumulator will
alway be less than 100 due to its initialization.
As for the second task:
Write another program that reads 100 numbers from the user and
prints out the sum.
We can use a similar series of steps as well as variables but the
*big* difference is that the condition does not depend on the user's
input at all, but rather the steps should occur a fixed number of
times (100).
In order to accomplish this, what steps do we do?
1) Define Variables
2) Initialization
3) Loop
These are the *same* steps as prior but there needs to be another
variable, a counter (in computer parlance, an "iterator", or "i" for
short). Here is step 1:
Define_Variables:
int input_num;
int accumulator;
int i; // iterator
Next comes the Initialization:
Initialization:
accumulator = 0;
i = 0;
And then loop. Again, we can use the second form of the loops
mentioned above because we know that our iterator, i, shall be less
than 100.
Loop:
{
std::cout << "Please enter a number: ";
std::cin >> input_num;
accumulator = accumulator + input_num;
// increase the iterator (running count)
i = i + 1;
}
Loops_Condition:
while(i < 100);
This is a set-up for a "for loop" that places the steps:
1) Initialization
2) Increase Iterator
3) Check Condition for loop
together:
for( INITIALIZATION ; CHECK CONDITION; ADJUST ITERATOR )
but the "ADJUST ITERATOR" step can really be anything, just something
that should change with each step. The for loop, however, is unlike
the:
do {
} while()
loop but is like the
while() {
}
loop as the condition is checked prior to the instructions are
performed.
Define_Variables:
int input_num;
int accumulator;
int i;
Initialization:
Loop:
Adjust Condition:
for(accumulator = 0, i = 0; i < 100; i = i + 1) {
std::cout << "Please enter a number: ";
std::cin >> input_num;
accumulator = accumulator + input_num;
}
Here are the steps of the "for loop":
1) Initialization (accumulator = 0, i = 0)
2) Check Condition (i < 100)
If condition is true
3) Do Instructions in body
4) Adjust iterator (i = i + 1)
5) Goto 2 (check condition *again*)
I hope this helps some and isn't as clear as mud. If it is just ignore
me! :-)
Elizabeth
More information about the Courses
mailing list