tryme3.py.xhtml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => 'tryme3.py',
  21. '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',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2>Script:</h2>
  25. <blockquote>
  26. <pre><code># Every good program should begin with a license comment that declares the file
  27. # as being covered by a free software license.
  28. #
  29. # This program is free software: you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation, either version 3 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. #
  39. # You should have received a copy of the GNU General Public License
  40. # along with this program. If not, see &lt;https://www.gnu.org./licenses/&gt;.
  41. # Prints one blank line
  42. #
  43. # The version of the function new_line() in the instructions looks like this:
  44. #
  45. ## def new_line():
  46. ## print()
  47. #
  48. # However, that version of the function doesn&apos;t work in Python2. In Python2, it
  49. # doesn&apos;t print blank lines. Instead, it prints the string representation of
  50. # empty tuples. That&apos;s obviously now what we want. So, I took out the
  51. # parentheses and made the function look like this:
  52. #
  53. ## def new_line():
  54. ## print
  55. #
  56. # Success! It now worked in Python2! So what was the problem? Well, it now no
  57. # longer worked in Python3! If I recall, &quot;print&quot; is a keyword in Python2, but a
  58. # function in Python3. In Python, functions are objects, and we all know that
  59. # objects are a type of variable. (In Python, all variables are objects, but
  60. # that&apos;s not the point. The point is that all functions are variables in
  61. # Python.) With out the parentheses, it was no longer a function call, but
  62. # simply an expression without an assignment! I ended up settling on the
  63. # implementation below, which works in both Python2 and Python3.
  64. def new_line():
  65. print(&quot;&quot;)
  66. # Prints three blank lines
  67. def three_lines():
  68. new_line()
  69. new_line()
  70. new_line()
  71. # Prints nine blank lines
  72. def nine_lines():
  73. three_lines()
  74. three_lines()
  75. three_lines()
  76. # Prints twenty-five blank lines
  77. # ( 9 + 9 + 3 + 3 + 1 == 25 )
  78. def clear_screen():
  79. nine_lines()
  80. nine_lines()
  81. three_lines()
  82. three_lines()
  83. new_line()
  84. # The assignment instructions say to define both nine_lines() and
  85. # clear_screen(), but only says to actually call clear_screen(). In theory,
  86. # this script should call clear_screen() but should avoid calling nine_lines().
  87. # However, the instructions also say to put separators between the output of the
  88. # functions. That seems to heavily imply that both functions get called. The
  89. # assignment also doesn&apos;t explicitly say *not* to call both functions, so I&apos;m
  90. # calling them both to be safe and hope that that&apos;s okay.
  91. #
  92. # This next part is pretty much documented by the calls to print(), so I&apos;m done
  93. # commenting now. I would live to have put a print statement after the call to
  94. # clear_screen() to contain the blank lines between visible lines, but the
  95. # instructions specifically state that the call to clear_screen() should be on
  96. # the final line.
  97. #
  98. # Oh wait, now that I think about it, it&apos;ll be contained by the prompt that
  99. # shows up after the script completes.
  100. print(&quot;now printing 9 lines&quot;)
  101. nine_lines()
  102. print(&quot;now printing 25 lines&quot;)
  103. clear_screen()</code></pre>
  104. </blockquote>
  105. <h2>Output:</h2>
  106. <blockquote>
  107. <pre>Python 2.7.9 (default, Jun 29 2016, 13:08:31)
  108. [GCC 4.9.2] on linux2
  109. Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information.
  110. &gt;&gt;&gt; ================================ RESTART ================================
  111. &gt;&gt;&gt;
  112. now printing 9 lines
  113. now printing 25 lines
  114. &gt;&gt;&gt;</pre>
  115. </blockquote>
  116. END
  117. );