[Courses] Re: need a little help with extremly basic python programming

Guru - haunter03 at hotmail.com
Sat Dec 7 13:41:54 EST 2002


ok, I worked out a really simple way to do the first one (after some 
advice...).
Thanks for all the help
>From: courses-request at linuxchix.org
>Reply-To: courses at linuxchix.org
>To: courses at linuxchix.org
>Subject: Courses digest, Vol 1 #254 - 8 msgs
>Date: Sat,  7 Dec 2002 06:27:04 +1100 (EST)
>
>Send Courses mailing list submissions to
>	courses at linuxchix.org
>
>To subscribe or unsubscribe via the World Wide Web, visit
>	http://mailman.linuxchix.org/mailman/listinfo/courses
>or, via email, send a message with subject or body 'help' to
>	courses-request at linuxchix.org
>
>You can reach the person managing the list at
>	courses-admin at linuxchix.org
>
>When replying, please edit your Subject line so it is more specific
>than "Re: Contents of Courses digest..."
>
>
>Today's Topics:
>
>    1. Re: [prog] Re: [Courses] need a little help with extremly basic 
>python programming (Elizabeth Barham)
>    2. Ok, is anyone else seeing this? (Morgon Kanter)
>    3. Re: Ok, is anyone else seeing this? (Elwing)
>    4. [C] Lesson 13: exc1 answer, problem on exc2 (Morgon Kanter)
>    5. Re: [C] Lesson 13: exc1 answer, problem on exc2 (Laurel Fan)
>    6. Re: Re: [C] Lesson 13: exc1 answer, problem on exc2 (Morgon Kanter)
>    7. Re: [C] lesson 13: debugging answer (Lorne Gutz)
>    8. Re: [C] Lesson 13: exc1 answer, problem on exc2 (Lorne Gutz)
>
>--__--__--
>
>Message: 1
>To: LinuxChix Programming <programming at linuxchix.org>
>Cc: LinuxChix Courses <courses at linuxchix.org>
>Subject: Re: [prog] Re: [Courses] need a little help with extremly basic 
>python programming
>From: Elizabeth Barham <lizzy at soggytrousers.net>
>Date: 05 Dec 2002 08:39:55 -0600
>
>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
>
>--__--__--
>
>Message: 2
>From: Morgon Kanter <admin at surgo.net>
>To: courses at linuxchix.org
>Date: Thu, 5 Dec 2002 15:59:31 -0500
>Subject: [Courses] Ok, is anyone else seeing this?
>
>It seems when I get my messages back in the digest form of linuxchix mail=
>, it=20
>when I view it there is an =3D20 after almost every line break (even the =
>ones =20
>I enter myself, with the enter key). I want to know - is anyone else seei=
>ng=20
>this, or is it something Kmail/linuxchix mailing list doing to my message=
>s?=20
>It is getting very annoying, and I'd love to know what I could do to stop=
>  it=20
>(I've already turned off smart quoting and stuff). I am really wondering=20
>what's up and if anyone else is seeing it.
>
>Morgon
>--=20
>grab my updated (11/25/02) public key from http://www.surgo.net/pubkey.as=
>c
>
>
>--__--__--
>
>Message: 3
>From: Elwing <elwing at elwing.org>
>To: Morgon Kanter <admin at surgo.net>, courses at linuxchix.org
>Subject: Re: [Courses] Ok, is anyone else seeing this?
>Date: Thu, 5 Dec 2002 16:15:08 -0500
>
>Are you using kmail?
>
>the =20 tends to appear in mailers that don't understand MIME quoted
>printable.  ( a lot of messages that I send to my boss (who uses emacs) 
>from
>kmail have the =20 because kmail (at least 3.0 and higher) supports it.
>
>most mailers (outlook, eudora, kmail, mozilla, etc...) send out MIME quoted
>printable.  Perhaps the mail system used by linuxchix doesn't understand it
>and so when it compiles the digest, it has the =20 on it?
>
>I don't receive the digest, and I don't see the =20 (which is probably 
>because
>linuxchix just forwards and doesn't try to interpret)
>
>just some ideas,
>Laura
>
>
>
>
>On Thursday 05 December 2002 03:59 pm, Morgon Kanter wrote:
> > It seems when I get my messages back in the digest form of linuxchix 
>mail,
> > it when I view it there is an =20 after almost every line break (even 
>the
> > ones I enter myself, with the enter key). I want to know - is anyone 
>else
> > seeing this, or is it something Kmail/linuxchix mailing list doing to my
> > messages? It is getting very annoying, and I'd love to know what I could 
>do
> > to stop it (I've already turned off smart quoting and stuff). I am 
>really
> > wondering what's up and if anyone else is seeing it.
> >
> > Morgon
>
>--
>Public key available at
>http://www.elwing.org/~elwing/elwing.gpg
>
>--__--__--
>
>Message: 4
>From: Morgon Kanter <admin at surgo.net>
>To: courses at linuxchix.org
>Date: Thu, 5 Dec 2002 17:58:08 -0500
>Subject: [Courses] [C] Lesson 13: exc1 answer, problem on exc2
>
>I'll start with my exercise 1 answer first. Unlike exc2, it uses file=20
>descriptors unneedingly.
>
>/********* BEGIN EXC 1 **********/
>#include <stdio.h>
>#include <errno.h>
>#include <fcntl.h>
>
>int main(int argc, char *argv[]) {
>     unsigned int number_of_lines =3D 0;
>     int fd;
>     FILE *file;
>     int input;
>
>     if(argc !=3D 2) {
>         fputs("Error: inproper number of arguments.\n", stderr);
>         fputs("Usage: ./lines filename\n", stderr);
>         return -1;
>     }
>
>     fd =3D open(argv[1], O_RDONLY);
>     if(errno =3D=3D ENOENT) {
>         fputs("Error: No such file by that name.\n", stderr);
>         return -1;
>     }
>     file =3D fdopen(fd, "r");
>
>     while(input !=3D EOF) {
>         input =3D getc(file);       =20
>         if(input =3D=3D 10) number_of_lines++;
>     }
>
>     printf("There were %i lines in %s\n", number_of_lines, argv[1]);=20
>
>     return 0;
>}
>/********** END **********/
>Output for this:
>=2E/exc1 blah
>There were 4 lines in blah (note: this is correct)
>=2E/exc1 yo
>Error: No such file by that name
>-----------------
>Okay, here is exercise 2. I'm having a bit of trouble in this one because=
>  in=20
>the output file, it copies correctly but it adds the character "y" with a=
>=20
>sideways-colon over top of it on a newline. Any ideas why?
>/************** BEGIN EXERCISE 2 ***************/
>/* All tabs will be expanded to FOUR spaces. Insane=20
>  * people who keep pushing for 8 space tabs!
>*/
>#include <stdio.h>
>#include <errno.h>
>
>int main(int argc, char *argv[]) {
>     FILE *file_read;
>     FILE *file_write;
>     int current_char;
>
>     if(argc !=3D 3) {
>         fprintf(stderr, "Error: Improper use of arguments.\n");
>         fprintf(stderr, "Usage: exc2 copyfile createdfile\n");
>         return -1;
>     }
>
>     file_read =3D fopen(argv[1], "r");
>     if(errno =3D=3D ENOENT) {
>         fprintf(stderr, "Error: No such file.\n");
>         return -1;
>     } else if(file_read =3D=3D NULL) {
>         fprintf(stderr, "Error: Wrong permissions on file-to-copy.\n");
>         return -1;
>     }
>    =20
>     file_write =3D fopen(argv[2], "w");
>     if(file_write =3D=3D NULL) {
>         fprintf(stderr, "Error: No write permissions.\n");
>         return -1;
>     }
>    =20
>     while(current_char !=3D EOF) {
>         current_char =3D fgetc(file_read);
>        =20
>         if(current_char =3D=3D 9) {
>             fputs("    ", file_write);
>         } else {
>             fputc(current_char, file_write);
>         }
>     }
>    =20
>     fclose(file_read);
>     file_read =3D NULL;
>     fclose(file_write);
>     file_write =3D NULL;
>
>     printf("Copied %s to %s with expanded tabs.\n", argv[1], argv[2]);
>        =20
>     return 0;
>}
>/************ END **************/
>
>Morgon
>--
>grab my updated (11/25/02) public key from http://www.surgo.net/pubkey.as=
>c
>
>
>--__--__--
>
>Message: 5
>Date: Thu, 5 Dec 2002 15:11:30 -0800
>From: Laurel Fan <laurel at sdf.lonestar.org>
>To: courses at linuxchix.org
>Subject: Re: [Courses] [C] Lesson 13: exc1 answer, problem on exc2
>
>On Thu, Dec 05, 2002 at 05:58:08PM -0500, Morgon Kanter wrote:
> > -----------------
> > Okay, here is exercise 2. I'm having a bit of trouble in this one 
>because in
> > the output file, it copies correctly but it adds the character "y" with 
>a
> > sideways-colon over top of it on a newline. Any ideas why?
>
>Here's a hint:
>
>Files do not really contain EOF.
>
>If that's not enough...
>.
>.
>.
>.
>.
>.
>.
>.
>.
>.
>.
>.
>.
>.
>
>Another hint:
>
>[snip]
> >     while(current_char != EOF) {
>First, You check if current_char is EOF.
> >         current_char = fgetc(file_read);
>Then you set current_char by reading it from the file.
>Could current_char == EOF here?
>
>Does that help?
>
>--
>laurel at sdf.lonestar.org
>http://dreadnought.gorgorg.org
>
>--__--__--
>
>Message: 6
>From: Morgon Kanter <admin at surgo.net>
>To: courses at linuxchix.org
>Date: Thu, 5 Dec 2002 18:39:44 -0500
>Subject: [Courses] Re: Re: [C] Lesson 13: exc1 answer, problem on exc2
>
>Thank you! That made me realize the problem, and I re-arranged my loop li=
>ke=20
>so:
>
>current_char =3D fgetc(file_read);
>while(current_char !=3D EOF) {
>     /* put stuff here */
>     current_char =3D fgetc(file_read);
>}
>
>Morgon
>--
>grab my updated (11/25/02) public key from http://www.surgo.net/pubkey.as=
>c
>
>
>--__--__--
>
>Message: 7
>From: Lorne Gutz <lgutz at vistar.ca>
>Reply-To: lgutz at vistar.ca
>Organization: Vistar Telecommunications Inc
>To: courses at linuxchix.org
>Subject: Re: [Courses] [C] lesson 13: debugging answer
>Date: Fri, 6 Dec 2002 08:20:10 -0500
>
>Although the function below works fine and does the job
>I would like to make two points:
>
>1.  string is a local variable in this function, changes made
>     here will not alter the variable string in the function that
>     called it.  So rather than introduce another variable 'i', why
>     not just use 'string' rather than 'string +i' ?  Then the line
>     i++; would become string++;   This reduces the complexity
>     just a little.
>
>2.  Functions should have one entery point and one exit.  To
>     accomplish this you will probably require a variable to hold
>     the return value.   Again things will work as written below
>     but a little self disipline goes a long way in simplifing code.
>
>Picky I know but now is the time you pick up bad habbits that
>will turn people off if you are job hunting.
>
>cheers
>Lorne
> >
> > /* Trim newline function:
> >  * Will trim the newline char in an inputted string.
> >  *
> >  * Returns 0 on success, and -1 on failure
> > */
> > int trim_newline(char *string) {
> >     unsigned int i =3D 0;
> >
> >     while(*(string + i) !=3D '\0') {
> >
> >         if(*(string + i) =3D=3D '\n') {
> >             *(string + i) =3D '\0';
> >             return 0;
> >         }
> >
> >         i++;
> >     }
> >
> >     return -1;
> > }
> > /******* END *******/
>
>
>
>--__--__--
>
>Message: 8
>From: Lorne Gutz <lgutz at vistar.ca>
>Reply-To: lgutz at vistar.ca
>Organization: Vistar Telecommunications Inc
>To: courses at linuxchix.org
>Subject: Re: [Courses] [C] Lesson 13: exc1 answer, problem on exc2
>Date: Fri, 6 Dec 2002 08:51:21 -0500
>
>Take a look at the man page on perror.  It is a very
>useful function when dealing with system errors.  It
>also saves a bit of time and effort once you have it
>mastered.
>
>cheers
>Lorne
>
>
> >
> >     fd =3D open(argv[1], O_RDONLY);
> >     if(errno =3D=3D ENOENT) {
> >         fputs("Error: No such file by that name.\n", stderr);
> >         return -1;
> >     }
> >     file =3D fdopen(fd, "r");
> >
> >     while(input !=3D EOF) {
> >         input =3D getc(file);       =20
> >         if(input =3D=3D 10) number_of_lines++;
> >     }
> >
> >     printf("There were %i lines in %s\n", number_of_lines, argv[1]);
> >
> >     return 0;
> > }
> > /********** END **********/
> >
>
>
>
>--__--__--
>
>_______________________________________________
>Courses mailing list
>Courses at linuxchix.org
>http://mailman.linuxchix.org/mailman/listinfo/courses
>
>
>End of Courses Digest


_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE* 
http://join.msn.com/?page=features/junkmail




More information about the Courses mailing list