Skip to content

Instantly share code, notes, and snippets.

@Suave
Created October 20, 2019 08:51
Show Gist options
  • Save Suave/79d06383d5522903ef5dc4b6b33dd3a6 to your computer and use it in GitHub Desktop.
Save Suave/79d06383d5522903ef5dc4b6b33dd3a6 to your computer and use it in GitHub Desktop.
very simple calculator demo for a friend
note = '''Note: You can use 'plus','minus','times','divide' and
'power' to calculate. Enter 'c' to clean and enter 'q'
to quit.'''
#note_1="Note: You can use 'plus','minus','times','divide' and "
#note_2="'power' to calculate. Enter 'c' to clean and enter 'q' "
#note_3="to quit."
#note=note_1+note_2+note_3
note_4="Enter a algorithm(+ - * / ^):"
note_5='Enter a algorithm:'
note_6='Clean! Enter a number:'
note_7='Error! Enter number only:'
note_8='Enter next algorithm:'
note_9='Enter next number:'
print(note)
test_n=0
def calc(x,a,y):
if a == '+':
u=x+y
elif a == '-':
u=x-y
elif a == '*':
u=x*y
elif a == '/':
u=x/y
elif a == '^':
u=x**y
return u
def test_number(x):
while x:
if x == 'c':
x=input(note_6)
continue
elif x == 'q':
exit()
try:
x=int(x)+5
x=x-5
return x
break
except ValueError:
x=input(note_7)
def test_algorithm(a,x):
while a:
if a == 'q':
exit()
elif a == 'c':
x=input(note_6)
test_number(x)
a=input(note_5)
continue
elif a == '+':
return a
break
elif a == '-':
return a
break
elif a == '*':
return a
break
elif a == '/':
return a
break
elif a == '**':
return a
break
else:
a=input(note_5)
continue
def test_next(y,a,x):
while y:
if y == 'q':
exit()
elif y == 'c':
x=input(note_6)
test_number(x)
return x
a=input(note_5)
test_algorithm(a,x)
return a
y=input(note_9)
continue
try:
y=int(y)+5
y=y-5
return y,a,x
break
except ValueError:
y=input(note_7)
x=input('Enter a number:')
x=test_number(x)
a=input(note_4)
a=test_algorithm(a,x)
y=input(note_9)
y=test_next(y,a,x)
outcome=calc(x,a,y)
print("Outcome is "+str(outcome))
while True:
x=outcome
a=input(note_5)
a=test_algorithm(a,x)
y=input(note_9)
y=test_next(y,a,x)
outcome=calc(x,a,y)
print('Outcome is '+str(outcome))
# -*- coding: utf-8 -*-
import os, sys
formula = [None, None, None]
result = 0
operator = None
prompt = '''Note: You can use 'plus','minus','times','divide' and
'power' to calculate. Enter 'c' to clean and enter 'q'
to quit.'''
def will_clean(opt):
if opt == 'c':
print("Cleaned.")
result = 0
formula[0] = None
return True
def will_quit(opt):
if opt == 'q':
print("Result:", result)
os._exit(0)
def is_operator(opt):
if opt not in ['+', '-', '*', '/']:
return False
return True
if __name__ == "__main__":
print(prompt)
while True:
for o in formula:
if o is not None:
print(o)
x = input("Enter a number or operator(+ - * /): ")
if will_clean(x):
continue
will_quit(x)
if not x.isdigit() and not is_operator(x):
print("Your input is invalid.")
continue
if formula[0] is None and not x.isdigit():
print("You should input a digital first.")
continue
if formula[0] is None and x.isdigit():
formula[0] = x # 为第一个算子赋值
continue
if formula[0].isdigit() and formula[1] is None:
if is_operator(x):
formula[1] = x # 为运算符赋值
if x.isdigit():
formula[0] = x # 更新第一个算子
continue
if formula[0].isdigit() and is_operator(formula[1]):
if is_operator(x):
formula[1] = x # 更新运算符
if x.isdigit():
formula[2] = x # 为第二个算子赋值
if formula[0].isdigit() and is_operator(formula[1]) and formula[2].isdigit():
if formula[1] == '+':
result = int(formula[0]) + int(formula[2])
if formula[1] == '-':
result = int(formula[0]) - int(formula[2])
if formula[1] == '*':
result = int(formula[0]) * int(formula[2])
if formula[1] == '/':
result = int(formula[0]) / int(formula[2])
print("%s %s %s = %s" % (formula[0], formula[1], formula[2], result))
formula = [str(result), None, None] # 计算结果变为第一个算子,等待新的算式
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment