Ok. This IS homework ...

Frederic Rentsch anthra.norell at vtxmail.ch
Mon Oct 16 05:15:01 EDT 2006


spawn wrote:
> but I've been struggling with this for far too long and I'm about to
> start beating my head against the wall.
>
> My assignment seemed simple: create a program that will cacluate the
> running total of user inputs until it hits 100.  At 100 it should stop.
>  That's not the problem, in fact, that part works.  It's the adding
> that isn't working.  How can my program add 2 + 7 and come up with 14?
>
> I'm posting my code (so that you may all laugh).  If ANYONE has any
> ideas on what I'm doing wrong, I'd appreciate.
>
> ---------------------------------------------------
>
> running = True
> goal = 100
>
> # subtotal = 0
> # running_total = subtotal + guess
>
> while running:
> 	guess = int(raw_input('Enter an integer that I can use to add : '))
> 	subtotal = guess
>
> 	while running:
> 		guess = int(raw_input('I\'ll need another number : '))
> 		running_total = guess +	subtotal
> 		print running_total
>
> 		if running_total == goal:
> 			print 'Congratulations!  You\'re done.'
>
> 		elif running_total > goal:
> 			print 'That\'s a good number, but too high.  Try again.'
>
> print 'Done'
>
> --------------------------
>
> I tried adding an additional "while" statement to capture the second
> number, but it didn't seem to solve my problem.  Help!
>
>   
Dear anonymous student,

Once upon a time programmers did things like this:

               BEGIN
                 |
  -------------->|<-------------------------------------
 |               |                                      |
 |           catch input                                |
 |               |                                      |
 |       input type valid? - prompt for correct input --|
 |               +                                      |
 |        input too large? + --- prompt for new input --
 |               -
 |      add to running total 
 |               |
 |          status report 
 |               |
  -- - running total >= max?
                 +
            report done  
                 |
                END

It was called a flow chart. Flow charts could be translated directly 
into machine code written in assembly languages which had labels, tests 
and jumps as the only flow-control constructs. When structured 
programming introduced for and while loops they internalized labeling 
and jumping. That was a great convenience. Flow-charting became rather 
obsolete because the one-to-one correspondence between flow chart and 
code was largely lost.
    I still find flow charting useful for conceptualizing a system of 
logical states too complex for my intuition. Everybody's intuition has a 
limit. Your homework solution shows that the assignment exceeds yours. 
So my suggestion is that you use the flow chart, like this:


def homework ():

   # Local functions. (I won't do those for you.)

   def explain_rules ():                                 
   def check_type (r):                                   
   def explain_type ():
   def check_size (r):
   def explain_max_size ():
   def report_status (rt):
   def report_done ():


   # Main function

   GOAL          = 100                               #                BEGIN
   MAX_INPUT     =  20                               #                  |
   running_total =   0                               #                  |
                                                     #                  |
   explain_rules ()                                  #                  |
                                                     #                  | 
   while 1:                                          #   
-------------->|<-------------------------------------
                                                     #  |               
|                                      |
      response = raw_input ('Enter a number > ')     #  |           
catch input                                |
                                                     #  |               
|                                      |
      if check_type (response) == False:             #  |       input 
type valid? - prompt for correct input --|
         explain_type ()                             #  |               
+                                      |
         continue                                    #  |               
|                                      |
                                                     #  |               
|                                      |
      if check_size (response) == False:             #  |        input 
too large? + --- prompt for new input --
         explain_max_size ()                         #  |               -
         continue                                    #  |               |
                                                     #  |               |
      running_total += int (response)                #  |      add to 
running total 
      report_status (running_total)                  #  |          
status report 
                                                     #  |               |
      if running_total >= GOAL:                      #   -- - running 
total >= max?
         break                                       #                  +
                                                     #                  |
   report_done ()                                    #             
report done
                                                     #                  |
   #? return (whatever)                              #                 END


Frederic

(... inviting you to sign your posts and to let me know the grade your 
teacher gives you for completing the local functions.)




More information about the Python-list mailing list