mycalc.py.xhtml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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' => 'mycalc.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-01',
  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
  27. # the file 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. # This makes Python2 behave like python3 in regards to division.
  42. from __future__ import division
  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. # User input is in string form, not number form. We need to not only
  47. # make sure the user input a number, but also convert the string
  48. # representation of that number into a numeric representation.
  49. def sanitize_and_parse(string):
  50. try:
  51. # If the input can be treated as an integer, we&apos;ll do that.
  52. return int(string)
  53. except:
  54. try:
  55. # If the input cannot be treated as an integer but can be treated as a
  56. # float, we&apos;ll do that instead.
  57. return float(string)
  58. except:
  59. # The assignment instructions say to disallow the use of zeros as
  60. # operands. Normally, invalid input shouldn&apos;t be treated as a valid
  61. # number, but we&apos;re rejecting zero anyway, so we might as well use it
  62. # as the return for bad input as well. That way, our zero-checker will
  63. # automatically filter out bad input.
  64. return 0
  65. # Display the equation thus far
  66. print(&quot;Equation: [?] [?] [?] == [?]&quot;)
  67. # Python doesn&apos;t have a &quot;do while&quot; loop construct. Let&apos;s instead use a
  68. # variable and a while loop.
  69. operand0 = 0
  70. while operand0 == 0:
  71. # Prompt for the first operand
  72. operand0 = input(&apos;Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.\nOperand 0: &apos;)
  73. # Clean up the first operand
  74. operand0 = sanitize_and_parse(operand0)
  75. if operand0 == 0:
  76. print(&quot;\t[ERROR]: Only non-zero numbers can be used as input.&quot;)
  77. # Display the equation thus far
  78. print(&quot;Equation: &quot; + str(operand0) + &quot; [?] [?] == [?]&quot;)
  79. operator = None
  80. while operator != &quot;+&quot; and operator != &quot;*&quot; and operator != &quot;-&quot; and operator != &quot;/&quot;:
  81. operator = input(&apos;Please enter an operator, then hit &quot;enter&quot;. Valid operators are &quot;+&quot; (addition), &quot;*&quot; (multiplication), &quot;-&quot; (subtraction), and &quot;/&quot; (division).\nOperator: &apos;)
  82. if operator != &quot;+&quot; and operator != &quot;*&quot; and operator != &quot;-&quot; and operator != &quot;/&quot;:
  83. print(&apos;\t[ERROR]: &quot;+&quot;, &quot;*&quot;, &quot;-&quot;, and &quot;/&quot; are the only supported operators.&apos;)
  84. # Display the equation thus far
  85. print(&quot;Equation: &quot; + str(operand0) + &quot; &quot; + operator + &quot; [?] == [?]&quot;)
  86. operand1 = 0
  87. while operand1 == 0:
  88. # Prompt for the second operand
  89. operand1 = input(&apos;Please provide the second operand (a non-zero number, if you will), then hit &quot;enter&quot;.\nOperand 1: &apos;)
  90. # Clean up the second operand
  91. operand1 = sanitize_and_parse(operand1)
  92. if operand1 == 0:
  93. print(&quot;\t[ERROR]: Only non-zero numbers can be used as input.&quot;)
  94. # Let&apos;s do some maths!
  95. if operator == &quot;+&quot;:
  96. result = operand0 + operand1
  97. elif operator == &quot;-&quot;:
  98. result = operand0 - operand1
  99. elif operator == &quot;*&quot;:
  100. result = operand0 * operand1
  101. else: # operator == &quot;/&quot;
  102. result = operand0 / operand1
  103. # Display the completed equation, including the result
  104. print(&quot;Equation: &quot; + str(operand0) + &quot; &quot; + operator + &quot; &quot; + str(operand1) + &quot; == &quot; + str(result))
  105. </code></pre>
  106. </blockquote>
  107. <h2>Output:</h2>
  108. <blockquote>
  109. <pre>Python 2.7.9 (default, Jun 29 2016, 13:08:31)
  110. [GCC 4.9.2] on linux2
  111. Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information.
  112. &gt;&gt;&gt; ================================ RESTART ================================
  113. &gt;&gt;&gt;
  114. Equation: [?] [?] [?] == [?]
  115. Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.
  116. Operand 0: 0
  117. [ERROR]: Only non-zero numbers can be used as input.
  118. Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.
  119. Operand 0: Five
  120. [ERROR]: Only non-zero numbers can be used as input.
  121. Please provide the first operand (a non-zero number, if you will), then hit &quot;enter&quot;.
  122. Operand 0: 215
  123. Equation: 215 [?] [?] == [?]
  124. Please enter an operator, then hit &quot;enter&quot;. Valid operators are &quot;+&quot; (addition), &quot;*&quot; (multiplication), &quot;-&quot; (subtraction), and &quot;/&quot; (division).
  125. Operator: q
  126. [ERROR]: &quot;+&quot;, &quot;*&quot;, &quot;-&quot;, and &quot;/&quot; are the only supported operators.
  127. Please enter an operator, then hit &quot;enter&quot;. Valid operators are &quot;+&quot; (addition), &quot;*&quot; (multiplication), &quot;-&quot; (subtraction), and &quot;/&quot; (division).
  128. Operator: /
  129. Equation: 215 / [?] == [?]
  130. Please provide the second operand (a non-zero number, if you will), then hit &quot;enter&quot;.
  131. Operand 1: 0
  132. [ERROR]: Only non-zero numbers can be used as input.
  133. Please provide the second operand (a non-zero number, if you will), then hit &quot;enter&quot;.
  134. Operand 1: 2
  135. Equation: 215 / 2 == 107.5
  136. &gt;&gt;&gt;</pre>
  137. </blockquote>
  138. END
  139. );