willsonlincake 发表于 2022-4-14 18:21:43

Python货币转换

https://www.geeksforgeeks.org/currency-converter-in-python/

willsonlincake 发表于 2022-4-14 18:21:48

import requests

class Currency_convertor:
    # empty dict to store the conversion rates
    rates = {}
    def __init__(self, url):
      data = requests.get(url).json()

      # Extracting only the rates from the json data
      self.rates = data["rates"]

    # function to do a simple cross multiplication between
    # the amount and the conversion rates
    def convert(self, from_currency, to_currency, amount):
      initial_amount = amount
      if from_currency != 'EUR' :
            amount = amount / self.rates

      # limiting the precision to 2 decimal places
      amount = round(amount * self.rates, 2)
      print('{} {} = {} {}'.format(initial_amount, from_currency, amount, to_currency))

# Driver code
if __name__ == "__main__":

    # YOUR_ACCESS_KEY = 'GET YOUR ACCESS KEY FROM fixer.io'
    url = str.__add__('http://data.fixer.io/api/latest?access_key=', YOUR_ACCESS_KEY)
    c = Currency_convertor(url)
    from
页: [1]
查看完整版本: Python货币转换