[Tutor] small program, but I'm very confused
Melissa K. Surprenant
msurprenant@siumed.edu
Tue Nov 5 14:51:01 2002
This is a multi-part message in MIME format.
--------------692419B4A77E2F23EE90E000
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
When I copied the code, I must have made a mistake - both lines are indented 5 spaces in
the actual program. I'm sorry. I just looked it over, and everything else copied
correctly.
Kalle Svensson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> [Melissa K. Surprenant]
> > I have to write a small program for class, and I'm having a terrible
> > time with it. I wrote the program, and it seems to me like it should
> > work, but it doesn't. I'm sure there are more errors than this, but
> > right now I get an error when I try to write to the file. It says I
> > have invalid syntax, with a carat point towards the file name for the
> > following line: f.write('\2\n')
> >
> > I would appreciate any help anyone would care to offer. Thank you!
> >
> > Here is the code:
> ...
> > for y in loginsList:
> > re.findall(('^loginsList[y]:x:[0-9]{4}:[0-9]{2,3}:)([A-Za-z]\s[A-Za-z])(.*?)(/n)',
> > '/etc/passwd')
> > f.write('\2\n')
> ...
>
> The thing is that Python uses indentation to determine scope. That
> is, to see if some code should be executed inside or outside a for
> loop, Python looks at how the code is indented.
>
> def func1():
> print "Hello",
>
> def func2():
> print "World!"
>
> for x in range(3):
> func1()
> func2()
>
> will result in the output
>
> Hello Hello Hello World!
>
> This means that the indentation must be consistent,
>
> for x in range(3):
> func1()
> func2()
>
> is wrong. Python can't determine if the call to func2 (which is
> indented 2 spaces) should be inside (indented 4 spaces) or outside
> (indented 0 spaces) the loop and instead of guessing raises a syntax
> error to give you a chance to correct the code.
>
> Peace,
> Kalle
> - --
> Kalle Svensson, http://www.juckapan.org/~kalle/
> Student, root and saint in the Church of Emacs.
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.0.7 (GNU/Linux)
> Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>
>
> iD8DBQE9yB8rdNeA1787sd0RAgiBAKCz7GjPB6A0qZDj7u8Tf9DvYi3M9wCfWQ1W
> 8ZDDXq0ZAMvMCPJBkYWwpzw=
> =bPmH
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
There are only 10 types of people in this world...those who understand binary, and those
who don't.
--------------692419B4A77E2F23EE90E000
Content-Type: text/x-vcard; charset=us-ascii;
name="msurprenant.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Melissa K. Surprenant
Content-Disposition: attachment;
filename="msurprenant.vcf"
begin:vcard
n:Surprenant;Melissa
tel;pager:217-467-3807
tel;fax:217-545-0192
tel;work:217-545-2132
x-mozilla-html:FALSE
org:SIU School of Medicine;Education & Curriculum
adr:;;801 N. Rutledge;Springfield;IL;62794-9622;United States
version:2.1
email;internet:msurprenant@siumed.edu
fn:Melissa K. Surprenant
end:vcard
--------------692419B4A77E2F23EE90E000--