Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- from forex_python.converter import CurrencyCodes, CurrencyRates
- HISTORY_PATH = "history.txt"
- def get_valuta(codes, msg):
- while True:
- print(msg)
- data = input()
- simbol = codes.get_symbol(data)
- if simbol is not None:
- return data
- print('Invalid valuta')
- def get_date():
- print('choose date (default today)')
- data = input()
- if not data:
- return None
- while True:
- print('write date in format %YYYY-%MM-%DD')
- data = input()
- try:
- d = datetime.datetime.strptime(data, '%Y-%m-%d')
- return d
- except Exception:
- print('Invalid date format')
- def get_money():
- print('how much money?')
- while True:
- data = input()
- try:
- money = float(data)
- return money
- except Exception:
- print('Invalid number')
- def convert():
- rates = CurrencyRates()
- codes = CurrencyCodes()
- name_from = get_valuta(codes, 'write valuta from')
- name_to = get_valuta(codes, 'write valuta to')
- date = get_date()
- money = get_money()
- rate = rates.get_rate(name_from, name_to)
- res = rates.convert(
- base_cur=name_from,
- dest_cur=name_to,
- amount=money,
- date_obj=date
- )
- msg = f'Конвертировал {money} {name_from} в {name_to} по курсу {rate} и получил {res} {name_to}'
- with open(HISTORY_PATH, 'a') as f:
- f.write(msg)
- print(msg)
- def get_history():
- print('-----History-----')
- try:
- with open(HISTORY_PATH) as f:
- for line in f:
- line = line.rstrip()
- print(line)
- print('-----------------')
- except IOError:
- print(f'Can not open file {HISTORY_PATH}, m/b history is empty')
- def main():
- info = '''
- There are 2 commands:
- 1) convert
- 2) history
- Choose one
- '''
- while True:
- print(info)
- cmd = input()
- if cmd == 'convert':
- convert()
- continue
- elif cmd == 'history':
- get_history()
- continue
- print('Invalid command')
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement