Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. import json
  2.  
  3. def write_history(a, b, c, result):
  4.     history = {'Number A': a, 'Number B': b, 'Operation': c, 'Result': result}
  5.     with open('history.json', 'at') as file:
  6.         json.dump(history, file, indent= " ")
  7.  
  8.     # file = open('history_2.txt', mode='at')
  9.     # lines = [str(a), ' ', str(c), ' ',str(b), ' = ', str(result), '\n', '-' * 15, '\n']
  10.     # file.writelines(lines)
  11.     # file.close()
  12.  
  13. def print_result(result):
  14.     print('-' * 5)
  15.     print('Результат: ', result)
  16.  
  17. def slojenie(a, b):
  18.     result = a + b
  19.     write_history(a, b, '+', result)
  20.     print_result(result)
  21.  
  22. def vichitanie(a, b):
  23.     result = a - b
  24.     write_history(a, b, '-', result)
  25.     print_result(result)
  26.  
  27. def umnojenie(a, b):
  28.     result = a * b
  29.     write_history(a, b, '*', result)
  30.     print_result(result)
  31.  
  32. def delenie(a, b):
  33.     result = a / b
  34.     write_history(a, b, '/', result)
  35.     print_result(result)
  36.  
  37. def stepen(a, b):
  38.     result = a ** b
  39.     write_history(a, b, '**', result)
  40.     print_result(result)
  41.  
  42. while True:
  43.     vibor = input("""Что хочешь сделать?\t
  44.    Calculator - 'k'\t
  45.    Help - 'i'\t
  46.    history_2 - 'h'\t
  47.    Exit - 'e'\t""")
  48.     if vibor == 'k':
  49.         action = input("Какую операцию хочешь совершить? (+ ,- ,* ,/ ,**)\n")
  50.         if action == 'e': break
  51.         elif action in ('+' ,'-' ,'*' ,'/' ,'**'):
  52.             a = int(input("Введи число a: "))
  53.             b = int(input("Введи число b: "))
  54.             if action == "+":
  55.                 slojenie(a, b)
  56.                 input()
  57.                 print('-' * 5)
  58.             elif action == "-":
  59.                 vichitanie(a, b)
  60.                 input()
  61.                 print('-' * 5)
  62.             elif action == "*":
  63.                 umnojenie(a, b)
  64.                 input()
  65.                 print('-' * 5)
  66.             elif action == "/" and b != 0:
  67.                 delenie(a, b)
  68.                 input()
  69.                 print('-' * 5)
  70.             elif action == "/" and b == 0:
  71.                 print("На ноль делить нельзя.")
  72.                 input()
  73.                 print('-' * 5)
  74.             elif action == "**":
  75.                 stepen(a, b)
  76.                 input()
  77.                 print('-' * 5)
  78.     elif vibor == 'i':
  79.         print(' Calculator v 1.1 \n Аффтар - Влад С. Вы можете отблагодарить разработчика обедом в маке :)')
  80.         input()
  81.     elif vibor == 'h':
  82.         print('-' * 60)
  83.         with open('history.json', 'rt') as file:
  84.             history = json.load(file)
  85.         print(history)
  86.         # file = open('history_2.txt', mode='rt')
  87.         # text = file.read()
  88.         # print(text)
  89.         # file.close()
  90.         input()
  91.     elif vibor == 'e': break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement