bool.py.xhtml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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' => 'bool.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-03-08',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2>Flowchart</h2>
  25. <img class="weblog-header-image" src="/img/CC_BY-SA_4.0/y.st./coursework/CS1101/bool.py.png" alt="A flowchart depicting the flow of the script below" width="625" height="1284" />
  26. <h2>Script:</h2>
  27. <blockquote>
  28. <pre><code># Every good program should begin with a license comment that declares the file
  29. # as being covered by a free software license.
  30. #
  31. # This program is free software: you can redistribute it and/or modify
  32. # it under the terms of the GNU General Public License as published by
  33. # the Free Software Foundation, either version 3 of the License, or
  34. # (at your option) any later version.
  35. #
  36. # This program is distributed in the hope that it will be useful,
  37. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. # GNU General Public License for more details.
  40. #
  41. # You should have received a copy of the GNU General Public License
  42. # along with this program. If not, see &lt;https://www.gnu.org./licenses/&gt;.
  43. # This makes Python2 behave like Python3 in regards to user input.
  44. if &quot;raw_input&quot; in vars(__builtins__):
  45. input = raw_input
  46. # This function compares two numbers and returns what is basically the three-way
  47. # equivalent of a boolean, but as an integer.
  48. def compare(a, b):
  49. if(a &gt; b):
  50. return 1
  51. elif(a == b):
  52. return 0
  53. # If a isn&apos;t greater than b and a isn&apos;t equal to b, a must be less than b.
  54. #There&apos;s no reason to even check to see if a is less than b.
  55. else:
  56. return -1
  57. # User input is in string form, not number form. We need to not only make sure
  58. # the user input a number, but also convert the string representation of that
  59. # number into a numeric representation.
  60. def sanitize_and_parse(string):
  61. try:
  62. # If the input can be treated as an integer, we&apos;ll do that.
  63. return int(string)
  64. except:
  65. try:
  66. # If the input cannot be treated as an integer but can be treated as a float,
  67. # we&apos;ll do that instead.
  68. return float(string)
  69. except:
  70. # If the input isn&apos;t numeric, we&apos;ll simply treat it as zero.
  71. return 0
  72. # This is a dummy function. We&apos;ll use it if we&apos;re in string mode.
  73. def return_argument(arg):
  74. return arg
  75. handle_input = None
  76. # This lets us toggle between string mode and numeric mode.
  77. while not handle_input:
  78. i = input(&apos;Enter &quot;0&quot; or &quot;s&quot; to compare strings.\\nEnter &quot;1&quot; or &quot;n&quot; to compare numbers.\\n&gt;&gt;&gt; &apos;)
  79. if i == &quot;0&quot; or i == &quot;s&quot;:
  80. handle_input = return_argument
  81. input_type = &quot;string&quot;
  82. elif i == &quot;1&quot; or i == &quot;n&quot;:
  83. handle_input = sanitize_and_parse
  84. input_type = &quot;number&quot;
  85. else:
  86. print(&quot;Invalid selection; please try again.&quot;)
  87. # We need to run the function three times.
  88. iterations = 3
  89. # Zero evaluates as False. &quot;while iterations&quot; will work as expected, we don&apos;t
  90. # need &quot;while iterations != 0&quot;.
  91. while iterations:
  92. a = input(&quot;Please enter the first &quot;+input_type+&quot;.\\n&gt;&gt;&gt; &quot;)
  93. a = handle_input(a)
  94. b = input(&quot;Please enter the second &quot;+input_type+&quot;.\\n&gt;&gt;&gt; &quot;)
  95. b = handle_input(b)
  96. # In Python2, the &quot;print&quot; key word won&apos;t output the correct message if we use
  97. # multiple arguments to print() without removing the parentheses. However,
  98. # removing the parentheses causes a syntax error in Python3. The only option
  99. # that seems to work in both versions of the language is to concatenate the two
  100. # things we need to print. We can&apos;t concatenate though without casting to string
  101. # first, so we also have to do that.
  102. print(&quot;Return code: &quot;+str(compare(a, b)))
  103. iterations -= 1</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. Enter &quot;0&quot; or &quot;s&quot; to compare strings.
  113. Enter &quot;1&quot; or &quot;n&quot; to compare numbers.
  114. &gt;&gt;&gt; n
  115. Please enter the first number.
  116. &gt;&gt;&gt; 5
  117. Please enter the second number.
  118. &gt;&gt;&gt; 2
  119. Return code: 1
  120. Please enter the first number.
  121. &gt;&gt;&gt; 2
  122. Please enter the second number.
  123. &gt;&gt;&gt; 5
  124. Return code: -1
  125. Please enter the first number.
  126. &gt;&gt;&gt; 3
  127. Please enter the second number.
  128. &gt;&gt;&gt; 3
  129. Return code: 0
  130. &gt;&gt;&gt;</pre>
  131. </blockquote>
  132. END
  133. );