This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
You haven't posted any discussions yet.
Here is the forum for Exercise 2.
My code for this exercise looks like this. I've spaced it so it is easier to read.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 # A comment, this is so you can read your program later.
2 # Any thing after the # is ignored by python.
3 print "I could have code like this." # and the comment after is ignored
4 # You can also use a comment to "disable" or comment out a piece of code:
5 # print "This won't run."
6 print "This will run."
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is the output.
C:\Python27\python ex2.py
I could have code like this.
This will run.
C:\Python27\
After seeing the # commenting, i went back and added notes (comments) to the other exercises.
So you could use the # in front of a line of code when trying to debug something? Sort of a way to turn a line on or off?
yup, it's magical how well it works, i remember doing it in BASIC, oh so many years ago. ;)
maybe I am dense, why does reading things backwards help?
It's a popular proofreading technique, see http://www.google.com/search?q=reading+backwards+proofreading
But after googling that, I discovered this paper that says it's not effective: http://eric.ed.gov/ERICWebPortal/search/detailmini.jsp?_nfpb=true&_&ERIC...
So if you find it's really not helping you, I'd say skip it :).
Hm, interesting! Perhaps it's not as useful on the first couple passes when the code/content is fresh but is useful when you've gone over it many times and start going into "skim" mode. Thanks for the links. :)
my code looks exactly like pborzel's code so I think I am doing something right :-). I do have a question though. In other programming languages like C++ and Java you can have multi line comments. They tend to start with /* and end with */ is there any way to do this in python?
The answer is ... sort of. The way to do this is to start and end with three double quotes i.e. """my multi-line comment""". This is also how you write multi-line strings. However, it's not a "real" comment in that it is interpreted by the compiler, see docstrings http://www.python.org/dev/peps/pep-0257/
For exercise 2, here is my code:
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out of piece of code:
# print "This won't run."
print "This will run."
Output:
$ python ex2.py
I could have code like this.
This will run.
The # for comments is common especially with bash .sh .pl and other scripting language. It makes it so easy to read and following through as to what that particular piece of script is doing.
Code:
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment is ignored
# You can use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."
Output:
Python 2.7.1 (r271:86882M, Nov 30 2010, 09:39:13)
[GCC 4.0.1 (Apple Inc. build 5494)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
I could have code like this.
This will run.
>>>
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."
=============
$ python ex2.py
I could have code like this.
This will run.
===
Did EC 1-4
Input:
# A comment, this is so you can read your program later.
# Anything after the # is ignored by python.
print "I could have code like this." # and the comment after is ignored
# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."
print "This will run."
My input looked like the others listed here, so I won't resubmit it. I placed my editor (gedit) directly over the pdf page of the exercise and read it aloud as I was typing (I live alone, nobody here but the cats will think I'm crazy). I nailed this one on the first try.