Advertisement
Guest User

converter_1602_ELIZAVETA_CHUPRAKOVA.py

a guest
Dec 11th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 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.  
  31. c = CurrencyRates()
  32. print("\tHELLO, dear user! My name is LISA and I want to present you my APPLICATION")
  33.  
  34. with open('get_history.txt', 'w') as f:
  35.   pass
  36.  
  37. while True:
  38.   print("\n Enter 1 to convert currency\n Enter 2 to get conversion history\n Enter 3 to exit the program")
  39.   command = input()
  40.   if command == '1':
  41.       print('\nEnter the currency')
  42.       first_curr = input().upper()
  43.       while not parse_curr_val(first_curr):
  44.           print('\nWrong format, enter the currency')
  45.           first_curr = input().upper()
  46.       print('\nEnter the currency you want to convert to')
  47.       second_curr = input().upper()
  48.       while not parse_curr_val(second_curr):
  49.           print('\nWrong format, enter the currency you want to convert to')
  50.           second_curr = input().upper()
  51.       print('\nHow much? Enter amount of currency')
  52.       amount = input()
  53.  
  54.       while not isfloat(amount):
  55.           print('\nError, please, enter digit value')
  56.           amount = input()
  57.       amount = float(amount)
  58.       print('\nWould you like to enter the date? Enter yes or no')
  59.       dd = input().lower()
  60.       while dd not in ['yes', 'no']:
  61.           print("\nERROR. Enter 'yes' or 'no'")
  62.           dd = input().lower()
  63.       if dd == 'yes':
  64.           print('\nEnter the date in format YYYY-MM-DD')
  65.           curr_date = input()
  66.  
  67.           while not date_valid(curr_date):
  68.               print("\nERROR. Enter the date in format YYYY-MM-DD")
  69.               curr_date = input()
  70.  
  71.  
  72.           curr_date = datetime.strptime(curr_date, '%Y-%m-%d').date()
  73.           itog = round(c.convert(first_curr, second_curr, amount, curr_date), 2)
  74.           with open('get_history.txt', 'a') as f:
  75.               f.write(first_curr + ' ' + second_curr + ' ' + str(amount) + ' ' \
  76.                       + str(curr_date) + ' ' + str(itog) + '\n')
  77.           print('\n Result of conversion --', itog)
  78.       elif dd == 'no':
  79.           itog = round(c.convert(first_curr, second_curr, amount), 2)
  80.           with open('get_history.txt', 'a') as f:
  81.               f.write(first_curr + ' ' + second_curr + ' ' + str(amount) + ' ' + str(itog) + '\n')
  82.           print('\n Result of conversion --', itog)
  83.   elif command == '2':
  84.       print('--- Conversion history ---\n')
  85.       with open('get_history.txt', 'r') as f:
  86.           print(f.read())
  87.   elif command == '3':
  88.       print('\nGoodbye!')
  89.       break
  90.   else:
  91.       print("\nERROR")
  92.       continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement