Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import datetime
  2.  
  3. from forex_python.converter import CurrencyCodes, CurrencyRates
  4.  
  5. HISTORY_PATH = "history.txt"
  6.  
  7.  
  8. def get_valuta(codes, msg):
  9. while True:
  10. print(msg)
  11. data = input()
  12.  
  13. simbol = codes.get_symbol(data)
  14. if simbol is not None:
  15. return data
  16.  
  17. print('Invalid valuta')
  18.  
  19.  
  20. def get_date():
  21. print('choose date (default today)')
  22. data = input()
  23. if not data:
  24. return None
  25.  
  26. while True:
  27. print('write date in format %YYYY-%MM-%DD')
  28. data = input()
  29. try:
  30. d = datetime.datetime.strptime(data, '%Y-%m-%d')
  31. return d
  32. except Exception:
  33. print('Invalid date format')
  34.  
  35.  
  36. def get_money():
  37. print('how much money?')
  38. while True:
  39. data = input()
  40. try:
  41. money = float(data)
  42. return money
  43. except Exception:
  44. print('Invalid number')
  45.  
  46.  
  47. def convert():
  48. rates = CurrencyRates()
  49. codes = CurrencyCodes()
  50.  
  51. name_from = get_valuta(codes, 'write valuta from')
  52. name_to = get_valuta(codes, 'write valuta to')
  53. date = get_date()
  54. money = get_money()
  55. rate = rates.get_rate(name_from, name_to)
  56.  
  57. res = rates.convert(
  58. base_cur=name_from,
  59. dest_cur=name_to,
  60. amount=money,
  61. date_obj=date
  62. )
  63. msg = f'Конвертировал {money} {name_from} в {name_to} по курсу {rate} и получил {res} {name_to}'
  64. with open(HISTORY_PATH, 'a') as f:
  65. f.write(msg)
  66.  
  67. print(msg)
  68.  
  69.  
  70. def get_history():
  71. print('-----History-----')
  72. try:
  73. with open(HISTORY_PATH) as f:
  74. for line in f:
  75. line = line.rstrip()
  76. print(line)
  77. print('-----------------')
  78. except IOError:
  79. print(f'Can not open file {HISTORY_PATH}, m/b history is empty')
  80.  
  81.  
  82. def main():
  83. info = '''
  84. There are 2 commands:
  85. 1) convert
  86. 2) history
  87.  
  88. Choose one
  89. '''
  90.  
  91. while True:
  92. print(info)
  93. cmd = input()
  94.  
  95. if cmd == 'convert':
  96. convert()
  97. continue
  98. elif cmd == 'history':
  99. get_history()
  100. continue
  101.  
  102. print('Invalid command')
  103.  
  104.  
  105. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement