123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * <https://y.st./>
- * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org./licenses/>.
- **/
- $xhtml = array(
- 'title' => 'tryme3.py',
- 'subtitle' => 'Written in <span title="Programming Fundamentals">CS 1101</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalized on 2017-02-22',
- 'copyright year' => '2017',
- 'body' => <<<END
- <h2>Script:</h2>
- <blockquote>
- <pre><code># Every good program should begin with a license comment that declares the file
- # as being covered by a free software license.
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org./licenses/>.
- # Prints one blank line
- #
- # The version of the function new_line() in the instructions looks like this:
- #
- ## def new_line():
- ## print()
- #
- # However, that version of the function doesn't work in Python2. In Python2, it
- # doesn't print blank lines. Instead, it prints the string representation of
- # empty tuples. That's obviously now what we want. So, I took out the
- # parentheses and made the function look like this:
- #
- ## def new_line():
- ## print
- #
- # Success! It now worked in Python2! So what was the problem? Well, it now no
- # longer worked in Python3! If I recall, "print" is a keyword in Python2, but a
- # function in Python3. In Python, functions are objects, and we all know that
- # objects are a type of variable. (In Python, all variables are objects, but
- # that's not the point. The point is that all functions are variables in
- # Python.) With out the parentheses, it was no longer a function call, but
- # simply an expression without an assignment! I ended up settling on the
- # implementation below, which works in both Python2 and Python3.
- def new_line():
- print("")
- # Prints three blank lines
- def three_lines():
- new_line()
- new_line()
- new_line()
- # Prints nine blank lines
- def nine_lines():
- three_lines()
- three_lines()
- three_lines()
- # Prints twenty-five blank lines
- # ( 9 + 9 + 3 + 3 + 1 == 25 )
- def clear_screen():
- nine_lines()
- nine_lines()
- three_lines()
- three_lines()
- new_line()
- # The assignment instructions say to define both nine_lines() and
- # clear_screen(), but only says to actually call clear_screen(). In theory,
- # this script should call clear_screen() but should avoid calling nine_lines().
- # However, the instructions also say to put separators between the output of the
- # functions. That seems to heavily imply that both functions get called. The
- # assignment also doesn't explicitly say *not* to call both functions, so I'm
- # calling them both to be safe and hope that that's okay.
- #
- # This next part is pretty much documented by the calls to print(), so I'm done
- # commenting now. I would live to have put a print statement after the call to
- # clear_screen() to contain the blank lines between visible lines, but the
- # instructions specifically state that the call to clear_screen() should be on
- # the final line.
- #
- # Oh wait, now that I think about it, it'll be contained by the prompt that
- # shows up after the script completes.
- print("now printing 9 lines")
- nine_lines()
- print("now printing 25 lines")
- clear_screen()</code></pre>
- </blockquote>
- <h2>Output:</h2>
- <blockquote>
- <pre>Python 2.7.9 (default, Jun 29 2016, 13:08:31)
- [GCC 4.9.2] on linux2
- Type "copyright", "credits" or "license()" for more information.
- >>> ================================ RESTART ================================
- >>>
- now printing 9 lines
- now printing 25 lines
- >>></pre>
- </blockquote>
- END
- );
|