Advertisement
KC358287

Untitled

Feb 5th, 2021
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import json
  2.  
  3. import requests
  4.  
  5. CURRENCIES = ["USD", "EUR", "GBP", "PLN"]
  6.  
  7.  
  8. def base_input():
  9.     """Getting action input."""
  10.     base = input('\nConvert from: ').upper()
  11.     return base
  12.  
  13.  
  14. def to_input():
  15.     """Getting action input."""
  16.     to = input('Convert to: ').upper()
  17.     return to
  18.  
  19.  
  20. def exchange_connector():
  21.     print('\nAvailable currencies: ')
  22.     print('-' * 60)
  23.     print(' '.join(CURRENCIES))
  24.     base = base_input()
  25.     if base in CURRENCIES:
  26.         url = (f'https://api.exchangeratesapi.io/latest?base={base}')
  27.         response = requests.get(url)
  28.         data = response.text
  29.         parsed = json.loads(data)
  30.         rates = parsed['rates']
  31.         exchange_rates(base, rates, parsed)
  32.         print('dsd')
  33.     elif base == '0':
  34.         quit('bye')
  35.     else:
  36.         print('There is no such currency in the database.\n')
  37.         exchange_connector()
  38.  
  39.  
  40. def exchange_rates(base, rates, parsed):
  41.     print(f'\nConversion rate per 100 {base}')
  42.     print('-' * 60)
  43.  
  44.     for currency, rate in rates.items():
  45.         if currency.strip().upper() in CURRENCIES:
  46.             print(f'{currency} = {round(100 * rate, 4)}')
  47.     date = parsed['date']
  48.     print('\ndate:', date)
  49.     print('-' * 60)
  50.  
  51.  
  52. exchange_connector()
  53.  
  54. # -----------------------------------------------------------------------------
  55.  
  56.  
  57. """Actual output:
  58. CAD = 128.0481
  59. HKD = 775.265
  60. ISK = 12926.6461
  61. PHP = 4810.2312
  62. DKK = 620.5625
  63. HUF = 29757.156
  64. CZK = 2153.5509
  65. GBP = 73.0518
  66. RON = 406.8013
  67. SEK = 844.7634
  68. IDR = 1403675.2065
  69. INR = 7290.9121
  70. BRL = 544.5047
  71. RUB = 7479.9716
  72. HRK = 631.0023
  73. JPY = 10574.9812
  74. THB = 3010.0142
  75. CHF = 90.3363
  76. EUR = 83.4516
  77. MYR = 407.0517
  78. BGN = 163.2146
  79. TRY = 707.277
  80. CNY = 647.0416
  81. NOK = 860.1185
  82. NZD = 139.9983
  83. ZAR = 1497.1793
  84. USD = 100.0
  85. MXN = 2031.9619
  86. SGD = 133.7979
  87. AUD = 131.528
  88. ILS = 329.2665
  89. KRW = 112279.8965
  90. PLN = 375.7239
  91.  
  92. date: 2021-02-05
  93. ------------------------------------------------------------
  94.  
  95. Process finished with exit code 0
  96. """
  97.  
  98. # ------------------------------------------------------------------------------------
  99.  
  100. """Proper output:
  101. USD = 100.0
  102. EUR = 83.4516
  103. GBP = 73.0518
  104. PLN = 375.7239
  105. """
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement