MeowalsoMeow

currency_converter_final

Aug 7th, 2021 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. '''
  2. Objectives
  3. You're in the bank. Think about how much and what kind of currency you have.
  4.  
  5. Take the currency code, the amount of money the user has, and the currency code that the user wants to receive as the user input.
  6. Retrieve the data from FloatRates as in the previous exercises.
  7. Save the exchange rates for USD and EUR.
  8. Read the currency to exchange for and the amount of money.
  9. Take a look at the cache. Maybe you already have what you need?
  10. If you have the currency in your cache, calculate the amount.
  11. If not, get it from the site, and calculate the amount.
  12. Save all the information to your cache.
  13. Print the results.
  14. Repeat steps 4-9 until there is no currency left to process.
  15. '''
  16. import requests
  17. import json
  18. # write your code here!
  19. # print('Meet a conicoin!')
  20. # print('Please, enter the number of conicoins you have:')
  21. #num = float(input())
  22. # print('Please, enter the exchange rate:')
  23. # rate = float(input())
  24. # print(f'The total amount of dollars: {num * rate}')
  25. # dollar = num * 100
  26. # print(f'I have {num} conicoins.')
  27. # print(f'{num} conicoins cost {num * 100} dollars')
  28. # print('I am rich! Yippee!')
  29. '''
  30. switcher = {
  31. 'RUB': 2.98,
  32. 'ARS': 0.82,
  33. 'HNL': 0.17,
  34. 'AUD': 1.9622,
  35. 'MAD': 0.208
  36. }
  37. li = [print(f'I will get {switcher[x] * num} {x} from the sale of {num} conicoins.') for x in switcher]
  38. '''
  39. choice = input().lower()
  40. if choice == '':
  41. exit()
  42. r = requests.get(f'http://www.floatrates.com/daily/{choice}.json')
  43. _dict = json.loads(r.text)
  44. # print(_dict)
  45. # print(_dict['usd'])
  46. # print(_dict['eur'])
  47. cache_dict = {}
  48. if choice == 'usd':
  49. cache_dict['eur'] = _dict['eur']
  50. elif choice == 'eur':
  51. cache_dict['usd'] = _dict['usd']
  52. else:
  53. cache_dict = {'usd': _dict['usd'], 'eur': _dict['eur']}
  54. # print(cache_dict)
  55. # print(_dict['usd'])
  56. # print(_dict['eur'])
  57.  
  58. while True:
  59. goal_currency = input().lower()
  60. if goal_currency == '':
  61. break
  62. money = input()
  63. if money == '':
  64. break
  65. else:
  66. money = float(money)
  67. print('Checking the cache...')
  68. if goal_currency in cache_dict:
  69. print('Oh! It is in the cache!')
  70. rate = cache_dict[goal_currency]['rate']
  71. print(f'You received {round(rate * money, 2)} {goal_currency.upper()}.')
  72. else:
  73. print('Sorry, but it is not in the cache!')
  74. cache_dict[goal_currency] = _dict[goal_currency]
  75. rate = cache_dict[goal_currency]['rate']
  76. print(f'You received {round(rate * money, 2)} {goal_currency.upper()}.')
  77.  
  78. '''rates = {}
  79. if source_code != 'usd':
  80. rates['usd'] = data['usd']['rate']
  81. if source_code != 'eur':
  82. rates['eur'] = data['eur']['rate']
  83. '''
Add Comment
Please, Sign In to add comment