Please Help: Currency Converter

Hello!

I have an assignment coming up where I have to create a currency converter using python, but I’m really confused on where or how to begin. Does anyone have any tips on how I could start? Like how would I make a simple window that displays two different currency slots? Any help is much appreciated, thank you!

Hello @bridgette

A good place to start is by writing out in pseudocode the steps that need to happen in order to solve the problem. At this stage you don’t need to know all of the syntax or algorithms to complete the task. You’re just outlining the logic or a roadmap that you are going to follow. Pseudocode will also help to clarify the particular area or areas of your question on how to create a currency converter.

*** Please take note, that since this is a homework question (and forum rules), we can supply guidance but not code solutions. I do not think you are asking for a solution but just want to explain why I have given more of a guidance answer.

By the way, I did a very quick search on YouTube, and there are several tutorials there that can get you started as well. :slight_smile:

:nerd_face:

Hi!

Thank you so much for replying. Yup, so at the beginning when I was doing research, I could only find tutorials and coding using Tkinter, but because I’ve never learnt how to use imported libraries (and I’m not even sure I’m allowed to use it for this assignment) I resorted to asking everyone here. What exactly is pseudocode?

Pseudocode examples here:
https://www.unf.edu/~broggio/cop2221/2221pseu.htm

Search engines are great for this type of question btw. :slight_smile:

1 Like

Perhaps you could ask your instructor whether you’re allowed to use Tkinter for the project? :slight_smile:

Converting one currency to another is the easy part – it’s just a multiplication operation. Building the graphic interface is the challenging bit.

To begin, consider: how does the user enter the currency values? Type numbers into a text input field? Press calculator-like buttons? …

Tkinter won’t work in Processing, but there are libraries like ControlP5 you can use instead.

1 Like

Yes, that’s basically how I was planning on doing it. Essentially, I wanted to have a window that has all the components of a currency converter


^^ To basically look like that
My assignment is meant to be creative and it’s not meant to be based on real currencies. So, not a lot of maths would be involved. But I’m just really stuck on creating the actual window itself. I tried using def setup and def draw functions but I’m still stuck

I’m not sure what you mean by “window” – Processing spawns a display window every time you run the code?

The way I see it, you need four Processing functions to make this work: setup() and draw() to structure the sketch; and mouseClicked() and keyPressed() to capture mouse and keyboard input. What follows is a basic scaffold.

The setup() and draw() look like this (I’m just drawing the input field here):

amount = ''  # text you see in the input field
fieldstroke = '#000000'  # stroke color for the input field
result = 0

def setup():
    size(500, 500)
    textSize(25)
    strokeWeight(3)

def draw():
    background('#999999')
    
    # draw text input field
    fill('#FFFFFF')
    stroke(fieldstroke)
    rect(40, 60, 150, 50)
    fill(0)
    text(amount, 55, 95)
    
    '''
    # draw result button
    fill('#0000FF')
    ...

The variables (amount, fieldstroke, result) control the ‘app’ state. The mouseClicked() function interacts with those variables to affect the state:

def mouseClicked():
    global fieldstroke, result
    
    # select input field when it's clicked
    if (mouseX > 40 and mouseX < 40+150 and 
        mouseY > 60 and mouseY < 60+50):
        fieldstroke = '#FF0000'
        
    '''
    # result (blue button) code
    if (mouseX > 200 and mouseX < 200+50 and 
        mouseY > 60 and mouseY < 60+50):
        ...

Now, for the keyPressed() section. If the field stroke is red, you can type text into it (by affecting the amount variable):

def keyPressed():
    global amount
    
    # if the input field has a red stroke, change the amount
    if fieldstroke == '#FF0000':
        amount += key

That should be enough to get you heading in the right direction.

1 Like

Thank you so much! this really helps

@bridgette

There are also examples to explore that come with the Processing software.

Add Python Mode, start a new sketch in Python mode and then go to menu and open:
File > Examples… >

image

:)

2 Likes