Skip to content

Instantly share code, notes, and snippets.

@onitonitonito
Created November 7, 2017 11:42
Show Gist options
  • Save onitonitonito/e153b785d100de29255bf387d9c20b92 to your computer and use it in GitHub Desktop.
Save onitonitonito/e153b785d100de29255bf387d9c20b92 to your computer and use it in GitHub Desktop.
test jupyter notebook viewer --- nbviewer.jupyter.org
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Calculator\n",
"\n",
"## 1.MarkDown\n",
"![__NOT__](http://americanpreppersnetwork.com/wp-content/uploads/2012/11/calculator.jpg)\n",
"[fig.1 using MarkDown]\n",
"\n",
"## 2.HTML TAG\n",
"> <img src='http://americanpreppersnetwork.com/wp-content/uploads/2012/11/calculator.jpg' width='200' align=left>\n",
"<br><br><br><br><br><br><br><br><br><br>\n",
"[fig.2 using HTML TAG]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# - Version 1.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a : 12\n",
"b : 34\n",
" What? (1=+, 2= x..)2\n",
"\n",
"\t\t12.0 - 34.0 = -22.0\n",
"\n",
"\n",
"a : 12\n",
"b : \n",
"could not convert string to float: \n",
"\n",
" ???? IndexError ????\n",
"\n",
"\n",
"\n",
"a : 12 ? 12\n",
"could not convert string to float: '12 ? 12'\n",
"\n",
" ???? IndexError ????\n",
"\n",
"\n",
"\n",
"a : 12\n",
"b : 12\n",
" What? (1=+, 2= x..)6\n",
"\n",
" ???? Missing Operand ???? \n",
"\n",
"\n",
"\n",
"\n",
" ???? IndexError ????\n",
"\n",
"\n",
"\n"
]
}
],
"source": [
"import math\n",
"\"\"\" function calculator \"\"\"\n",
"\n",
"def get_input_args(): # IN=x / OUT= 2'float', 1'int'\n",
" \"\"\" get 3 inpits preventing break due to an Error \"\"\"\n",
" # When -- NORMAL\n",
" try:\n",
" _arg1 = float(input('a : '))\n",
" _arg2 = float(input('b : '))\n",
" _operand = int(input(' What? (1=+, 2= x..)'))\n",
" return _arg1, _arg2, _operand\n",
"\n",
" # When -- ABNORMAL (ERROR OCCURES)\n",
" except ValueError as e:\n",
" print(e)\n",
" _arg1 = _arg2 = 0\n",
" _operand = 1\n",
" return _arg1, _arg2, _operand\n",
"\n",
"def get_plus_ab(a, b): # IN= 2'int' / OUT= 1'int'\n",
" \"\"\" return a+b while a = int, b = int \"\"\"\n",
" return a+b\n",
"\n",
"def get_minus_ab(a, b): # IN= 2'int' / OUT= 1'int'\n",
" \"\"\" return a-b while a = int, b = int \"\"\"\n",
" return a-b\n",
"\n",
"\n",
"while True:\n",
" # Input function -- called\n",
" _arg1, _arg2, _operand = get_input_args()\n",
"\n",
" # Error check -- error_flag, on/off\n",
" if _arg1 == 0 and _arg2 == 0 and _operand == 1:\n",
" _error_flag = True\n",
" else:\n",
" _error_flag = False\n",
"\n",
" # choose operand type\n",
" if _operand == 1:\n",
" c = get_plus_ab(_arg1, _arg2)\n",
" operand ='+'\n",
"\n",
" elif _operand == 2:\n",
" c = get_minus_ab(_arg1, _arg2)\n",
" operand ='-'\n",
"\n",
" else:\n",
" _error_flag = True\n",
" print('\\n ???? Missing Operand ???? \\n\\n\\n')\n",
"\n",
" # show result\n",
" if not _error_flag:\n",
" print('\\n\\t\\t{} {} {} = {:,}\\n\\n'.format(_arg1, operand, _arg2, c))\n",
" else:\n",
" print('\\n ???? IndexError ????\\n\\n\\n')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Calculator\n",
"## - modify input functions (by equation string)\n",
"\n",
"> <img src='http://americanpreppersnetwork.com/wp-content/uploads/2012/11/calculator.jpg' width='200' align=left>\n",
"<br><br><br><br><br><br><br><br><br><br>\n",
"[fig.2 using HTML TAG]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# - Version 2.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input Command String Separate by spaces for each..\n",
"('num1' [+-*/**] 'num2' = ) \n",
"10 / 3.33\n",
"\n",
"\t\t10.0 / 3.33 = 3.003003003003003\n",
"\n",
"\n",
"Input Command String Separate by spaces for each..\n",
"('num1' [+-*/**] 'num2' = ) \n",
" 12 ?? 12\n",
"\n",
" ???? Missing Operand ???? \n",
"\n",
"\n",
"\n",
"\n",
" ???? IndexError ????\n",
"\n",
"\n",
"\n",
"Input Command String Separate by spaces for each..\n",
"('num1' [+-*/**] 'num2' = ) \n",
"12 / \n",
"list index out of range\n",
"\n",
" ???? IndexError ????\n",
"\n",
"\n",
"\n",
"Input Command String Separate by spaces for each..\n",
"('num1' [+-*/**] 'num2' = ) \n",
"12 ** 12\n",
"\n",
"\t\t12.0 ** 12.0 = 8,916,100,448,256.0\n",
"\n",
"\n",
"Input Command String Separate by spaces for each..\n"
]
}
],
"source": [
"import math\n",
"\"\"\" function calculator\n",
" - input modified. \"\"\"\n",
"\n",
"def get_input_args(): # IN=x / OUT= 3'int\n",
" \"\"\" get 3 inpits preventing break due to an Error \"\"\"\n",
" # When -- NORMAL\n",
" command_arr = input(\"('num1' [+-*/**] 'num2' = ) \\n\").strip().split()\n",
"\n",
" try:\n",
" _arg1 = float(command_arr[0])\n",
" _operand = command_arr[1] # 'str'\n",
" _arg2 = float(command_arr[2])\n",
" return _arg1, _arg2, _operand\n",
"\n",
" # When -- ABNORMAL (ERROR OCCURES)\n",
" except IndexError as e:\n",
" print(e)\n",
" _arg1 = _arg2 = 0\n",
" _operand = '+'\n",
" return _arg1, _arg2, _operand\n",
"\n",
"def get_plus_ab(a, b): # IN= 2 num / OUT= 1 num\n",
" \"\"\" return a+b while a = int, b = int \"\"\"\n",
" return a+b\n",
"\n",
"def get_minus_ab(a, b): # IN= 2 num / OUT= 1 num\n",
" \"\"\" return a-b while a = int, b = int \"\"\"\n",
" return a-b\n",
"\n",
"def get_multi_ab(a, b): # IN= 2 num / OUT= 1 num\n",
" \"\"\" return a*b while a = int, b = int \"\"\"\n",
" return a*b\n",
"\n",
"def get_divide_ab(a, b): # IN= 2 num / OUT= 1 num\n",
" \"\"\" return a/b while a = int, b = int \"\"\"\n",
" return a/b\n",
"\n",
"def get_square_ab(a, b): # IN= 2 num / OUT= 1 num\n",
" \"\"\" return a*b while a = int, b = int \"\"\"\n",
" return a**b\n",
"\n",
"def is_error(_arg1, _arg2, _operand): # Out= True or False\n",
" \"\"\" if exception, values are automatically assigned and return True \"\"\"\n",
" if _arg1 == 0 and _arg2 == 0 and _operand == '+':\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"\n",
"while True:\n",
" print(\"Input Command String Separate by spaces for each..\")\n",
" _arg1, _arg2, _operand = get_input_args()\n",
" _error_flag = is_error(_arg1, _arg2, _operand)\n",
"\n",
" # print(get_plus_ab.__doc__)\n",
"\n",
" if _operand == '+':\n",
" c = get_plus_ab(_arg1, _arg2)\n",
"\n",
" elif _operand == '-':\n",
" c = get_minus_ab(_arg1, _arg2)\n",
"\n",
" elif _operand == '*':12\n",
" c = get_multi_ab(_arg1, _arg2)\n",
"\n",
" elif _operand == '/':\n",
" c = get_divide_ab(_arg1, _arg2)\n",
"\n",
" elif _operand == '**':\n",
" c = get_square_ab(_arg1, _arg2)\n",
"\n",
" else:\n",
" _error_flag = True\n",
" print('\\n ???? Missing Operand ???? \\n\\n\\n')\n",
"\n",
" # show result\n",
" if not _error_flag:\n",
" print('\\n\\t\\t{} {} {} = {:,}\\n\\n'.format(_arg1, _operand, _arg2, c))\n",
" else:\n",
" print('\\n ???? IndexError ????\\n\\n\\n')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment