-1

Hello i'm learning python and i am trying to make a small program that converts money into dollar,euro or British pounds. Can some one help me and tell me why is not working? Thanks!!!

def calculate():
    currency_input = input("Insert value:")
    dollar = 34
    euro = 36
    pound = 52
    select_currency = input("Insert currency(dollar,euro or pound):")   
    if select_currency is "dollar":
        currency_input  * dollar
    elif select_currency is "euro":
        currency_input * euro
    elif select_currency is "pound":
        currency_input * pound
    else:
        print ("Please select a currency(dollar,euro,pound)!")
    calculate()
calculate()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Victor
  • 468
  • 1
  • 5
  • 17

2 Answers2

0

You are testing for identity, not equality. Use == instead:

if select_currency == "dollar":

is tests if the name select_currency is referring to the same object; two objects can be distinct but still have the same value, which you'd test for with ==.

You need to fix all your string tests, and also actually store the result of your calculation:

if select_currency == "dollar":
    result = currency_input  * dollar
elif select_currency == "euro":
    result = currency_input * euro
elif select_currency == "pound":
    result = currency_input * pound

Easier still would be to use a dictionary here:

currencies = {
    'dollar': 34,
    'euro': 36,
    'pound': 52,
}
if select_currency in currencies:
    result = currency_input * currencies[select_currency]
else:
    print ("Please select a currency(dollar,euro,pound)!")
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

you should use == instead of is because is will not do what you think it does in this case. More on that here.

Use .lower() to allow the user to also enter Dollars and still succeed.

It seems that you want to be able to handle when the user inputs invalid information. You should use a try except block to make sure the user enter only numbers for the currency_input

Use while True loops to keep on asking the user for correct input. If they enter correct input we stop asking with the break statement.

Dictionaries make it easy to store the currency name and their associated value.

Also the math is pretty the same for all currencies, the only thing that changes is the value of the currency (dollars, euro, ...) so we can just look up what the user chose and than multiply that times the currency_input

def calculate():
    # we only want the user to input numbers
    while True:
        try:
            currency_input = float(input('Insert value: '))  # input always returns a str, we need to type cast
            break  # if input is valid we break out of the loop and move on
        except TypeError:  # handle the error when the input is not a number
            print('Please enter a number.')

    # use a dictionary because it is easier to read
    currency_dict = {
        'dollar': 34,
        'euro': 36,
        'pound': 52}

    # get the type of currency and do the math
    while True:
        select_currency = input('Insert currency(dollar,euro or pound): ').lower()
        if select_currency not in currency_dict:  # if the users enter something that is not in the dict
            print('Invalid currency')  # oops, try again
        else:
            money = currency_input * currency_dict[select_currency]  # we do the math
            return money  # return allows us to further manipulate that variable if we so desire 

print(calculate())

Thanks to Martijn Pieters for pointing out two improvements.

Community
  • 1
  • 1
Vader
  • 6,335
  • 8
  • 31
  • 43