[Tutor] Hello and a (probably) stupid question

Chris Readle patterner@rocketmail.com
Tue Jul 1 17:39:02 2003


Reggie,

Aha, that sure enough did it.  I was looking at the code, and comparing
them to each other, but I never thought about comparing the
whitespace....this is one of those things that will take me awhile to wrap
my head around I think. :)  Thanks for the quick response!

chris

--- Reggie Dugard <reggie@merfinllc.com> wrote:
> Chris,
> 
> Hi, glad to hear you're interested in Python!  Your problem illustrates
> the importance of indentation in Python.  In your interactive example,
> you'll notice that the else clause is indented to the same level as the
> for statement.  This means that the statements in the else block will be
> executed if the for loop is allowed to run to completion (no break is
> encountered).
> 
> In the example you entered in gvim, the else clause is indented to the
> level of the if statement so it will be executed whenever the if test
> fails during a pass through the loop - giving you incorrect results.
> 
> I hope this explanation is somewhat clear.  Good luck with the rest of
> the tutorial!
> 
> On Tue, 2003-07-01 at 13:37, Chris Readle wrote:
> > Hi all, 
> > 
> > My name is Chris and I've just started learning Python.  I'm working
> > through the tutorial and I've come upon a difficult thing.  One of the
> > sample pieces of code works fine when typed interactively into the
> > interpreter, but doesn't seem to work when I type it up in gvim.  Here
> are
> > the two bits of code:
> > 
> > Bit that works:
> > for n in range(2,10):
> >     for x in range(2, n):
> >         if n % x == 0:
> >             print n, 'equals', x, '*', n/x
> >             break
> >     else:
> >         # Loop fell through without finding factor
> >         print n, 'is a prime number'
> > 
> > This bit returns:
> > 2 is a prime number
> > 3 is a prime number
> > 4 equals 2 * 2
> > 5 is a prime number
> > 6 equals 2 * 3
> > 7 is a prime number
> > 8 equals 2 * 4
> > 9 equals 3 * 3
> > 
> > Which the tutorial indicates is correct.
> > 
> > 
> > Bit that doesn't work:
> > # Testing to see if there is some funny starting value in the
> variables
> > causing this to fail
> > n, x = 0, 0
> > print n, x
> > 
> > for n in range(2,10):
> > 	for x in range(2, n):
> > 		if n % x == 0:
> > 			print n, 'equals', x, '*', n/x
> > 			break
> > 		else:
> > 			# Loop fell through without finding a factor
> > 			print n, 'is a prime number'
> > 
> > This bit returns:
> > 0 0
> > 3 is a prime number
> > 4 equals 2 * 2
> > 5 is a prime number
> > 5 is a prime number
> > 5 is a prime number
> > 6 equals 2 * 3
> > 7 is a prime number
> > 7 is a prime number
> > 7 is a prime number
> > 7 is a prime number
> > 7 is a prime number
> > 8 equals 2 * 4
> > 9 is a prime number
> > 9 equals 3 * 3
> > 
> > Which is a little messed.
> > 
> > Now, the only thing I see that's different is that they're indented
> > differently.  However, both are indented consistently, which is my
> > understanding of how that should work.  To add some kick to the sauce,
> if
> > I take the former example and paste it into a text file and run THAT
> > through the interpreter, it works as well.
> > 
> > Any thoughts from the python experts out there?
> > 
> > chris
> > 
> > 
> > __________________________________
> > Do you Yahoo!?
> > SBC Yahoo! DSL - Now only $29.95 per month!
> > http://sbc.yahoo.com
> > 
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> -- 
> Reggie
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com