📜  Python中的货币转换器

📅  最后修改于: 2022-05-13 01:54:34.930000             🧑  作者: Mango

Python中的货币转换器


Python是一种非常通用的编程语言。几乎所有主流技术都在使用Python ,并且几乎可以使用它开发任何应用程序。让我们看一个Python程序,将一个国家的货币转换为另一个国家的货币。要使用此服务,必须需要 API 密钥,可在此处获取。
我们将使用fixer API来获取实时转换率并转换相应的金额。

需要的模块:

requests:此模块不内置于Python。要安装它,请在终端或 cmd 中键入以下命令。

pip install requests

下面是实现:

# Python program to convert the currency
# of one country to that of another country 
  
# Import the modules needed
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[from_currency]
  
        # limiting the precision to 2 decimal places
        amount = round(amount * self.rates[to_currency], 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_country = input("From Country: ")
    to_country = input("TO Country: ")
    amount = int(input("Amount: "))
  
    c.convert(from_country, to_country, amount)

输入 :

From Country: USD 
TO Country: INR 
Amount: 1 

输出 :

1 USD = 70.69 INR