DEV Community

Sabin Sim
Sabin Sim

Posted on

My Project #1: Building a Currency Converter (Python & Streamlit)

πŸš€ Project Phase Begins: From Syntax to Solutions

We have finished the "Python Basics" series! πŸŽ‰ We learned about variables, if statements, functions, and input handling.

But syntax alone doesn't make you a developer. Building things does.

From today, I will start the "Mini-Project Series." We will take the concepts we learned and apply them to create actual working applications.

Our first project is a classic: A Currency Converter.
We will build it in two ways:

  1. Console Version: Using basic Python logic.
  2. Web App Version: Using Streamlit to give it a Graphical User Interface (GUI).

1️⃣ The Goal: CHF ↔ EUR Converter

Living in or traveling near Switzerland often means dealing with two currencies: Swiss Francs (CHF) and Euros (EUR).

Instead of Googling the rate every time, let's write a script that does the math for us.

Key Logic:

  • CHF to EUR: Multiply by the rate.
  • EUR to CHF: Divide by the rate.

[Image of currency exchange concept]


2️⃣ Step 1: The Console Version (Backend Logic)

First, let's implement the core logic using input() and print(). This is the raw logic without a fancy interface.

πŸ“„ converter.py

# Fixed exchange rate (Simulated)
rate = 1.05

choice = input("Select an option: 1) CHF β†’ EUR | 2) EUR β†’ CHF: ")

if choice == "1":
    print("=== Currency Converter ===")
    # Get user input and convert string to float
    amount = float(input("Enter amount in CHF: "))
    result = amount * rate
    print(f"EUR: {result:.2f}")

elif choice == "2":
    print("=== Currency Converter ===")
    amount = float(input("Enter amount in EUR: "))
    # Division for reverse conversion
    result = amount / rate
    print(f"CHF: {result:.2f}")

else:
    print("Invalid option.")
Enter fullscreen mode Exit fullscreen mode

What we applied here:

  • input() to get user data.
  • float() to handle decimal numbers (money).
  • if-elif-else to handle the direction of conversion.

3️⃣ Step 2: The Web Version (Frontend with Streamlit)

Console apps are great for logic, but users prefer buttons and text boxes.

We don't need to learn HTML/CSS yet. We can use a Python library called Streamlit to turn our script into a web app in seconds.

πŸ“„ converter_streamlit.py

import streamlit as st

# 1. Title
st.title("πŸ’± Currency Converter (CHF ↔ EUR)")

rate = 1.05  # Exchange rate

# 2. Radio button for user choice
option = st.radio(
    "Select conversion direction:",
    ("CHF β†’ EUR", "EUR β†’ CHF")
)

# 3. Numeric Input
amount = st.number_input("Enter amount:", min_value=0.0, format="%.2f")

# 4. Button to trigger calculation
if st.button("Convert"):
    if option == "CHF β†’ EUR":
        result = amount * rate
        st.success(f"{amount} CHF = {result:.2f} EUR")
    else:
        result = amount / rate
        st.success(f"{amount} EUR = {result:.2f} CHF")
Enter fullscreen mode Exit fullscreen mode

4️⃣ Comparison: Console vs. Streamlit

Feature Console Version (input) Web Version (streamlit)
Interface Black terminal window Clean Web Browser UI
Input Typing text and pressing Enter Clicking buttons and typing boxes
Feedback Text output (print) Visual boxes (st.success)
Difficulty Very Easy Easy (Requires library installation)

5️⃣ How to Run This Project

If you want to try this code on your machine:

1. Console Version

Simply run it with Python:

python3 converter.py
Enter fullscreen mode Exit fullscreen mode

2. Streamlit Version

First, install the library:

pip install streamlit
Enter fullscreen mode Exit fullscreen mode

Then run the app:

streamlit run converter_streamlit.py
Enter fullscreen mode Exit fullscreen mode

A browser tab will open automatically!


πŸ”§ Future Improvements

This project is a great start, but it has one limitation: The exchange rate is hardcoded (fixed at 1.05).

In a real-world application, exchange rates change every second.
Next steps for this project:

  1. Connect to a Real-time API to fetch the current rate.
  2. Add Multi-currency support (USD, JPY, KRW).
  3. Deploy the web app so others can use it via a URL.

This was a simple start to our project journey. Stay tuned for the next project where we tackle more complex logic!

Github Repository: [Link to your actual Github Repo]





Enter fullscreen mode Exit fullscreen mode

Top comments (0)