Search code examples
pythonpython-3.xfunctioncurrencyconverters

How can I make sure that my currency converter displays if above clip limit? (Python)


I am trying to create a converter for currencies that keeps into consideration clip limit by countries.

I went as far as creating a function that displays status of clip level according to a fix variable:

input1 = str(input('Enter Currency [CZK or PLN]: '))
value1 = float(input('Enter Value: '))

if input1 == 'PLN' or 'Pln' or 'pln':
    value2 = value1/3
    if input1 == 'CZK' or 'Czk' or 'czk':
        value2 = value1/20
else: print('Some error occurred, try again.')

round(value2,2)
print('The conversion of ', value1, input1, 'is: ', value2,'USD \n')

if value2 >= 20:
    print('ABOVE Clip level - EU PO is required')
else: print('BELOW clip level')
print('value ',value2,'USD matches expected.')

As you can see the code is quite rudimentary and take into consideration made-up conversion rates. What I would like to achieve is that the function returns that value is ABOVE/BELOW clip level according to a chosen country. So for example I have in place CKZ and PLN as currencies, therefore I would use CZ and PL).

Such function is not implemented in the code yet because I am not sure how to proceed. Would you be able to help? Thank you!

Edit > run code by Younes: Output


Solution

  • This line here :

    else: print('Some error occurred, try again.')
    

    requires a loop to start with. You want to create a function and then call it inside a loop, this might do :

    def f(country,value):
    
        if "PL" in country.upper():
            value2 = value/3
        elif "CZ" in country.upper():
            value2 = value/20
        else: 
            raise ValueError
        round(value2,2)
        print('The conversion of ', value1, input1, 'is: ', value2,'USD \n')
    
        if value2 >= 20:
            print('ABOVE Clip level - EU PO is required')
            return('ABOVE Clip level')
        else: 
            print('value {} USD matches expected.'.format(value2))
            return('BELOW clip level')
        
    while True:
    
        input1 = str(input('Enter Currency [CZK or PLN]: '))
        value1 = float(input('Enter Value: '))
    
        try :
            f(input1,value1)
            break
        except ValueError:
            print('Some error occurred, try again.')