frustrated stupid newbie question

Sheila King usenet at thinkspot.net
Tue Mar 12 19:25:31 EST 2002


[posted and mailed]

On Tue, 12 Mar 2002 17:59:54 -0600, "Scott Kurland" <skurland at juggler.net>
wrote in comp.lang.python in article <u8t5kbhc6qq587 at corp.supernews.com>:

> > > Why isn't this $%^*#$% program working?
> >
> > > #Searching for perfect numbers
> > >
> > > howhigh= input ("How high should I check?")
> > > for number in range (1,howhigh):
> > >  factorsum = 0
> > >  halfnumber=number/2
> > >  for checking in range (1,halfnumber):
> > >   if number/checking == int (number/checking):
> > >    factorsum = factorsum + checking
> > >  if number == factorsum:
> > >   print number
> > >
> >
> > What do you mean it isn't working? It ran without crashing when I tried
> it.
> > Here is the output from a sample run:
> >
> > >>>
> > How high should I check?50
> > 10
> >
> > Are you expecting some other type of output? What are you expecting?
> 
> Well, I was expecting failure, which we got.  What I hoped for was the two
> perfect numbers between 1 and 50, 6 and 28.

You really should play with the interpreter window open, or add more print
statements to your code so that you can see what is happening at all of the
intermediate steps. Consider the data in this interactive session:

>>> howhigh = 10
>>> range(1,howhigh)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, howhigh + 1)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> for number in range(1, howhigh + 1):
	print "for number = ", number,
	print "halfnumber = ", number/2

	
for number =  1 halfnumber =  0
for number =  2 halfnumber =  1
for number =  3 halfnumber =  1
for number =  4 halfnumber =  2
for number =  5 halfnumber =  2
for number =  6 halfnumber =  3
for number =  7 halfnumber =  3
for number =  8 halfnumber =  4
for number =  9 halfnumber =  4
for number =  10 halfnumber =  5
>>> 

Is this what you expect of the data in the intermediate steps?

> > > Flame away, I can't feel dumber than this.
> >
> > Oh, now what newsgroup mistreated you so, that you would expect such a
> > thing? We don't do that here in comp.lang.python!

> Goodness! I'll have to mind my manners carefully, then; usenet has roughened
> them some.
> 
> Thank you, Sheila.

Are you saying, that we need to watch out for flames from you??? 
Oh, dear!
(donning my asbestos suit...)

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list