Chatgpt

Chatgpt 이용한 프로그램 짜기 6 - 파이썬 환율 변환기

code2772 2023. 7. 27. 20:18
728x90
반응형

✔ GPT 입력

:  파이썬으로 환율 변환기를 만들어줘 가입이 필요 없는 방식으로

pip install requests

 

✔ GPT 가 작성한 코드

import requests


def get_exchange_rate(base_currency, target_currency):
    url = f"https://api.exchangerate-api.com/v4/latest/{base_currency}"
    response = requests.get(url)
    data = response.json()
    return data["rates"].get(target_currency)


def currency_converter(amount, base_currency, target_currency):
    exchange_rate = get_exchange_rate(base_currency, target_currency)
    if not exchange_rate:
        return None
    return amount * exchange_rate


if __name__ == "__main__":
    print("Currency Converter")
    print("===================")

    base_currency = input("Enter the base currency (e.g., USD, EUR, GBP): ").upper()
    target_currency = input("Enter the target currency (e.g., USD, EUR, GBP): ").upper()
    amount = float(input("Enter the amount to convert: "))

    converted_amount = currency_converter(amount, base_currency, target_currency)
    if converted_amount is not None:
        print(
            f"{amount} {base_currency} is equivalent to {converted_amount:.2f} {target_currency}."
        )
    else:
        print("Currency conversion failed. Please check the currencies you entered.")

 

✔ 결과

 

반응형