123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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' => 'mycalc.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-03-01',
- '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/>.
- # This makes Python2 behave like python3 in regards to division.
- from __future__ import division
- # This makes Python2 behave like Python3 in regards to user input.
- if "raw_input" in vars(__builtins__):
- input = raw_input
- # User input is in string form, not number form. We need to not only
- # make sure the user input a number, but also convert the string
- # representation of that number into a numeric representation.
- def sanitize_and_parse(string):
- try:
- # If the input can be treated as an integer, we'll do that.
- return int(string)
- except:
- try:
- # If the input cannot be treated as an integer but can be treated as a
- # float, we'll do that instead.
- return float(string)
- except:
- # The assignment instructions say to disallow the use of zeros as
- # operands. Normally, invalid input shouldn't be treated as a valid
- # number, but we're rejecting zero anyway, so we might as well use it
- # as the return for bad input as well. That way, our zero-checker will
- # automatically filter out bad input.
- return 0
- # Display the equation thus far
- print("Equation: [?] [?] [?] == [?]")
- # Python doesn't have a "do while" loop construct. Let's instead use a
- # variable and a while loop.
- operand0 = 0
- while operand0 == 0:
- # Prompt for the first operand
- operand0 = input('Please provide the first operand (a non-zero number, if you will), then hit "enter".\nOperand 0: ')
- # Clean up the first operand
- operand0 = sanitize_and_parse(operand0)
- if operand0 == 0:
- print("\t[ERROR]: Only non-zero numbers can be used as input.")
- # Display the equation thus far
- print("Equation: " + str(operand0) + " [?] [?] == [?]")
- operator = None
- while operator != "+" and operator != "*" and operator != "-" and operator != "/":
- operator = input('Please enter an operator, then hit "enter". Valid operators are "+" (addition), "*" (multiplication), "-" (subtraction), and "/" (division).\nOperator: ')
- if operator != "+" and operator != "*" and operator != "-" and operator != "/":
- print('\t[ERROR]: "+", "*", "-", and "/" are the only supported operators.')
- # Display the equation thus far
- print("Equation: " + str(operand0) + " " + operator + " [?] == [?]")
- operand1 = 0
- while operand1 == 0:
- # Prompt for the second operand
- operand1 = input('Please provide the second operand (a non-zero number, if you will), then hit "enter".\nOperand 1: ')
- # Clean up the second operand
- operand1 = sanitize_and_parse(operand1)
- if operand1 == 0:
- print("\t[ERROR]: Only non-zero numbers can be used as input.")
- # Let's do some maths!
- if operator == "+":
- result = operand0 + operand1
- elif operator == "-":
- result = operand0 - operand1
- elif operator == "*":
- result = operand0 * operand1
- else: # operator == "/"
- result = operand0 / operand1
- # Display the completed equation, including the result
- print("Equation: " + str(operand0) + " " + operator + " " + str(operand1) + " == " + str(result))
- </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 ================================
- >>>
- Equation: [?] [?] [?] == [?]
- Please provide the first operand (a non-zero number, if you will), then hit "enter".
- Operand 0: 0
- [ERROR]: Only non-zero numbers can be used as input.
- Please provide the first operand (a non-zero number, if you will), then hit "enter".
- Operand 0: Five
- [ERROR]: Only non-zero numbers can be used as input.
- Please provide the first operand (a non-zero number, if you will), then hit "enter".
- Operand 0: 215
- Equation: 215 [?] [?] == [?]
- Please enter an operator, then hit "enter". Valid operators are "+" (addition), "*" (multiplication), "-" (subtraction), and "/" (division).
- Operator: q
- [ERROR]: "+", "*", "-", and "/" are the only supported operators.
- Please enter an operator, then hit "enter". Valid operators are "+" (addition), "*" (multiplication), "-" (subtraction), and "/" (division).
- Operator: /
- Equation: 215 / [?] == [?]
- Please provide the second operand (a non-zero number, if you will), then hit "enter".
- Operand 1: 0
- [ERROR]: Only non-zero numbers can be used as input.
- Please provide the second operand (a non-zero number, if you will), then hit "enter".
- Operand 1: 2
- Equation: 215 / 2 == 107.5
- >>></pre>
- </blockquote>
- END
- );
|