Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.58 KB | None | 0 0
  1. from forex_python.converter import CurrencyRates
  2. from datetime import datetime
  3.  
  4.  
  5. def parse_curr_val(value):
  6.     c = CurrencyRates()
  7.     curr_list = c.get_rates('USD').keys()
  8.     if value in curr_list:
  9.         return True
  10.     else:
  11.         return False
  12.  
  13.  
  14. def isfloat(value):
  15.     try:
  16.         float(value)
  17.         return True
  18.     except ValueError:
  19.         return False
  20.  
  21.  
  22. def date_valid(date):
  23.     try:
  24.         datetime.strptime(date, '%Y-%m-%d')
  25.         return True
  26.     except ValueError:
  27.         return False
  28.  
  29.  
  30. def main():
  31.     c = CurrencyRates()
  32.     print("\tHELLO, dear user! My name is BIBA and I like to present you my APPLICATION")
  33.     # здесь можешь написать приветственное сообщение для пользователя с описанием своего
  34.     # охуенного приложения, которое выведется один раз в начале, дальше будет бескончный цикл, который не закочится
  35.     # пока ебанный пользователь не введет 3 и не завершит работу программы
  36.  
  37.     # создаем ПУСТОЙ файл для истории конвертаций. Каждый раз заново, что важно понимать
  38.     # записывает историю конвертаций во время работы приложения
  39.     # можем сделать чтобы писал постоянно туда
  40.     with open('get_history.txt', 'w') as f:
  41.         pass
  42.  
  43.     while True:
  44.         print("\n Enter 1 to convert currency\n Enter 2 to get conversion history\n Enter 3 to exit the program")
  45.         command = input()
  46.         if command == '1':
  47.             print('\nEnter the currency')
  48.             first_curr = input().upper()
  49.             while not parse_curr_val(first_curr):
  50.                 print('\nWrong format, enter the currency')
  51.                 first_curr = input().upper()
  52.             print('\nEnter the currency you want to convert to')
  53.             second_curr = input().upper()
  54.             while not parse_curr_val(second_curr):
  55.                 print('\nWrong format, enter the currency you want to convert to')
  56.                 second_curr = input().upper()
  57.             print('\nHow much? Enter amount of currency')
  58.             amount = input()
  59.  
  60.             while not isfloat(amount):
  61.                 print('\nError,please, enter digit value')
  62.                 amount = input()
  63.             amount = float(amount)
  64.             print('\nWould you like to enter the date? Enter yes or no')
  65.             dd = input().lower()
  66.             while dd not in ['yes', 'no']:
  67.                 print("\nERROR. Enter 'yes' or 'no'")
  68.                 dd = input().lower()
  69.             if dd == 'yes':
  70.                 print('\nEnter the date in format YYYY-MM-DD')
  71.                 curr_date = input()
  72.  
  73.                 while not date_valid(curr_date):
  74.                     print("\nERROR. Enter the date in format YYYY-MM-DD")
  75.                     curr_date = input()
  76.  
  77.                 """                
  78.                ВАЖНО!!! Замечено, что прога вылетает из каких то дат
  79.                из за внутренней ошибки фортекса ебаного
  80.                RatesNotAvailableError: Currency Rates Source Not Ready
  81.                """
  82.  
  83.                 curr_date = datetime.strptime(curr_date, '%Y-%m-%d')
  84.                 # print(curr_date)
  85.                 itog = round(c.convert(first_curr, second_curr, amount, curr_date), 2)
  86.                 with open('get_history.txt', 'a') as f:
  87.                     f.write(first_curr + ' ' + second_curr + ' ' + str(amount) + ' ' \
  88.                             + str(curr_date) + ' ' + str(itog) + '\n')
  89.                 print('\n Result of conversion --', itog)
  90.             elif dd == 'no':
  91.                 itog = round(c.convert(first_curr, second_curr, amount), 2)
  92.                 with open('get_history.txt', 'a') as f:
  93.                     f.write(first_curr + ' ' + second_curr + ' ' + str(amount) + ' ' + str(itog) + '\n')
  94.                 print('\n Result of conversion --', itog)
  95.         elif command == '2':
  96.             print('--- Consersion history ---\n')
  97.             with open('get_history.txt', 'r') as f:
  98.                 print(f.read())
  99.         # показать историю
  100.         elif command == '3':
  101.             print('\nGoodbye!')
  102.             break
  103.         # завершить прогу
  104.         # exit()
  105.         else:
  106.             print("\nERROR")
  107.             continue
  108.  
  109.  
  110. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement