-2

I am assigned to make a currency converter and I am not sure why it isn't working. This is the program:

print(
    "Welcome to Currency Converter"
)

USD = input(
    "What amount of USD would you like to convert to another currency? Write only the number and click enter "
)

Currency = input(
    "Which currency would you like to convert USD from? (Options: The Canadian Dollar(CAD), The Swiss Franc(CHF), The Japanese Yen(JPY), or The British Pound(GBP) PLEASE ENTER 3 LETTER CURRENCY CODE IN UPPERCASE"
)

if Currency == "CAD":
    print(USD * 1.252910,
        " Canadian Dollars")

if Currency == "CHF":
    print(USD * 0.94122, " Swiss Franc")

if Currency == "JPY":
    print(USD * 109.29, " Japanese Yen")

if Currency == "GBP":
    print(USD * 0.71832,
        "British Pounds")

This question has been resolved I had not defined "USD" as an int before the input

  • 2
    Please elaborate on what isn't working, where you think the broken logic is, and what you've tried. – Brien Foss Feb 10 '18 at 18:56
  • 1
    Your input USD is a string, not a number. You have to convert it into an integer with `int()`, before you can perform mathematical calculations with this variable. And chances are that you will benefit from reading [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Feb 10 '18 at 19:01
  • 1
    `USD = int(input(...))` – Keyur Potdar Feb 10 '18 at 19:01
  • 1
    Please don't post pictures of code or error messages on SO, post the text directly here in your question. And as we said: Your input in Python 3 is a string, you have to tell [Python to treat it as a number.](https://www.tutorialspoint.com/python3/python_numbers.htm) – Mr. T Feb 10 '18 at 19:05
  • Thank you for helping me with the type error. My program now works but I have a follow up question. Should I have this program be a def and if so how do I go about that. I thought I begin by having it be as follows def CurrencyConverter (put all the code here) CurrencyConverter() Will this prevent the prompt from closing instantly after running the code. – Joey Pilotte Feb 10 '18 at 19:09

1 Answers1

2

I took the liberty to remake your program with some "improvements".

  • Textwrap.dedent to format text nicely
  • Dictionary to hold the currency values
  • Functions to make sure you get valid input

Here:

from textwrap import dedent

curr_dict = {'CAD':1.252910,
             'CHF':0.94122,
             'JPY':0.71832,
             'GBP':109.29}

def float_input(text):
    while True:
        try:
            return float(input(text))
        except ValueError:
            print('Not a number! Insert correct value.')

def conversion_func(text):
    while True:
        currency = input(text)
        if currency in curr_dict:
            return curr_dict.get(currency)
        else:
            print('Not a valid currency. Try again.')

def main():
    '''
    Asks for amount to convert (USD) and desired currency
    Prints the result
    '''
    amount = float_input(dedent('''\
        Welcome to Joseph Pilotte's Currency Converter
        What amount of USD would you like to convert to another currency?
        Write only the number and click enter\n'''))

    conversion = conversion_func(dedent('''\n\
        Which currency would you like to convert USD from?
        (Options: The Canadian Dollar(CAD), The Swiss Franc(CHF), 
                  The Japanese Yen(JPY) or The British Pound(GBP)
        PLEASE ENTER 3 LETTER CURRENCY CODE IN UPPERCASE'''))

    print(amount*conversion)

main()
Anton vBR
  • 18,287
  • 5
  • 40
  • 46