搜索
首页后端开发Python教程python 中的货币转换器

Currency Converter in python

大家好,今天我将向您展示如何使用 Python 和 Flask 创建一个简单的货币转换器并将其显示在网络上。

首先我们需要确保 Flask 已正确安装,为此,在 Windows 中打开 Powershell 或 CMD,并确保以管理员身份运行,右键单击它,然后以管理员身份运行,Flask 很容易通过键入以下内容安装命令:

pip 安装烧瓶

安装Python之后,当然你可以查看这个链接来了解如何在Windows上安装Python:

(https://www.geeksforgeeks.org/how-to-install-python-on-windows/)

成功安装Flask后,创建一个名为currency_converter的文件夹,并在该文件夹内创建一个名为app.py的txt文件,并确保将扩展名从.txt更改为.py,然后在currency_converter文件夹内创建另一个名为(templates)的文件夹并制作确保该文件夹的名称与 templates 完全相同,否则 Flask 将无法运行,然后在 templates 文件夹中创建一个名为 index.html 的文件,您可以创建一个 txt 文件,然后将其重命名为 index.html 并确保扩展名是.html

这是app.py文件的代码:

from flask import Flask, render_template, request, redirect, url_for
import requests

app = Flask(__name__)

API_URL = "https://api.exchangerate-api.com/v4/latest/{}"

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "POST":
        from_currency = request.form["from_currency"].upper()
        to_currency = request.form["to_currency"].upper()
        amount = float(request.form["amount"])

        # Fetch exchange rate data
        response = requests.get(API_URL.format(from_currency))
        if response.status_code == 200:
            data = response.json()
            rates = data.get("rates", {})
            if to_currency in rates:
                conversion_rate = rates[to_currency]
                converted_amount = amount * conversion_rate
                return render_template(
                    "index.html",
                    converted_amount=converted_amount,
                    from_currency=from_currency,
                    to_currency=to_currency,
                    amount=amount,
                )
            else:
                error = f"Currency '{to_currency}' not found."
                return render_template("index.html", error=error)
        else:
            error = f"Error fetching data for '{from_currency}'."
            return render_template("index.html", error=error)

    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True)

这是带有 css 的 HTML 文件的代码:



    
    
    <title>货币转换器</title>
    
        /* 一般身体造型 */
        身体 {
            字体系列:'Arial',无衬线字体;
            保证金:0;
            填充:0;
            背景:线性渐变(135deg,#6dd5fa,#2980b9);
            颜色: 白色;
            显示:柔性;
            调整内容:居中;
            对齐项目:居中;
            高度:100vh;
        }

        /* 使容器居中 */
        。容器 {
            背景:#ffffff10; /* 半透明白色 */
            边框半径:10px;
            内边距:20px 30px;
            最大宽度:400px;
            宽度:100%;
            盒子阴影:0 8px 16px rgba(0, 0, 0, 0.3);
            文本对齐:居中;
        }

        /* 标题样式 */
        h1 {
            字体大小:28px;
            下边距:20px;
            颜色:#fff;
            文本阴影:1px 1px 4px rgba(0, 0, 0, 0.8);
        }

        /* 输入和按钮样式 */
        输入,按钮 {
            显示:块;
            宽度:100%;
            边距:10px 0;
            内边距:12px;
            字体大小:16px;
            边框:无;
            边框半径:5px;
        }

        输入 {
            背景:#ffffff80; /* 半透明白色 */
            颜色:#333;
        }

        按钮 {
            背景:#2980b9;
            颜色:#fff;
            字体粗细:粗体;
            光标:指针;
            过渡:背景0.3s缓动;
        }

        按钮:悬停{
            背景:#1e5786;
        }

        /* 结果消息样式 */
        。结果 {
            背景:红色;
            内边距:10px;
            边框半径:5px;
            顶部边距:20px;
            文本阴影:1px 1px 2px rgba(0, 0, 0, 0.7);
        }

        .结果 p {
            保证金:0;
            字体大小:18px;
        }

        /* 错误消息样式 */
        。错误 {
            颜色:#ff4d4d;
            边距:10px 0;
            字体粗细:粗体;
        }

        /* 响应式设计 */
        @media(最大宽度:480px){
            。容器 {
                内边距:15 像素 20 像素;
            }

            h1 {
                字体大小:22px;
            }

            输入,按钮 {
                字体大小:14px;
            }
        }
    风格>
头>

    <div>



<p>然后打开 Powershell 或 CMD 并导航到您的currency_converter 文件夹位置并输入:<br>
烧瓶运行</p>

<p>这将在您的计算机上创建一个网络服务器,其 IP 和端口号如下:</p>

<p>http://127.0.0.1:5000</p>
<p>打开网络浏览器,然后复制该地址并将其粘贴到您的浏览器中,然后尝试货币转换器。</p>

<p>有关代码的货币列表,请查看此网站:</p>

<p>(https://taxsummaries.pwc.com/glossary/currency-codes)</p>

<p>非常享受并感谢您。</p>


          </div>

            
        

以上是python 中的货币转换器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

对于循环和python中的循环时:每个循环的优点是什么?对于循环和python中的循环时:每个循环的优点是什么?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器