[Tutor] Hello and a (probably) stupid question

Chris Readle patterner@rocketmail.com
Tue Jul 1 16:38:02 2003


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