123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?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' => 'bool.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-08',
- 'copyright year' => '2017',
- 'body' => <<<END
- <h2>Flowchart</h2>
- <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" />
- <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 user input.
- if "raw_input" in vars(__builtins__):
- input = raw_input
- # This function compares two numbers and returns what is basically the three-way
- # equivalent of a boolean, but as an integer.
- def compare(a, b):
- if(a > b):
- return 1
- elif(a == b):
- return 0
- # If a isn't greater than b and a isn't equal to b, a must be less than b.
- #There's no reason to even check to see if a is less than b.
- else:
- return -1
- # 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:
- # If the input isn't numeric, we'll simply treat it as zero.
- return 0
- # This is a dummy function. We'll use it if we're in string mode.
- def return_argument(arg):
- return arg
- handle_input = None
- # This lets us toggle between string mode and numeric mode.
- while not handle_input:
- i = input('Enter "0" or "s" to compare strings.\\nEnter "1" or "n" to compare numbers.\\n>>> ')
- if i == "0" or i == "s":
- handle_input = return_argument
- input_type = "string"
- elif i == "1" or i == "n":
- handle_input = sanitize_and_parse
- input_type = "number"
- else:
- print("Invalid selection; please try again.")
- # We need to run the function three times.
- iterations = 3
- # Zero evaluates as False. "while iterations" will work as expected, we don't
- # need "while iterations != 0".
- while iterations:
- a = input("Please enter the first "+input_type+".\\n>>> ")
- a = handle_input(a)
- b = input("Please enter the second "+input_type+".\\n>>> ")
- b = handle_input(b)
- # In Python2, the "print" key word won't output the correct message if we use
- # multiple arguments to print() without removing the parentheses. However,
- # removing the parentheses causes a syntax error in Python3. The only option
- # that seems to work in both versions of the language is to concatenate the two
- # things we need to print. We can't concatenate though without casting to string
- # first, so we also have to do that.
- print("Return code: "+str(compare(a, b)))
- iterations -= 1</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 ================================
- >>>
- Enter "0" or "s" to compare strings.
- Enter "1" or "n" to compare numbers.
- >>> n
- Please enter the first number.
- >>> 5
- Please enter the second number.
- >>> 2
- Return code: 1
- Please enter the first number.
- >>> 2
- Please enter the second number.
- >>> 5
- Return code: -1
- Please enter the first number.
- >>> 3
- Please enter the second number.
- >>> 3
- Return code: 0
- >>></pre>
- </blockquote>
- END
- );
|