Can I correlate specific divisor value (float) to specific value (string) on a list?

Viewed 52

I have this specific code which is meant to first convert specific currency into EUR value. This part of code has been sorted out:

country = ['CZ', 'PL']
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. BELOW Clip level'.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.')

You will notice that the retrieved value enters a second condition: if value2 >= 20:

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

I would like to achieve a correlation between the country (or currency) and a specific value. So, let's say if I would like to have: value2 = 20 as limit for country = PL; value2 = 10 as limit for country = CZ.

I understood that this should be somehow integrated into the function f but I cannot figure out how. I have tried to nest it as well as write it separately but it does not return the correct value. I guess I could create a list of clip level but seems confusing. The other option was to include a table with correspondence of value per country but honestly, I have no idea if that is possible. Finally, the last option (the one I am trying right now and still seem not working) is to nest a function cliplimit within the function f:

def cliplimit(cliplim):
    for country in f:
        if country == 'PL':
            cliplim = 20
        elif country == 'CZ':
            cliplim = 10
        else:
            raise ValueError
        round(cliplim,0)
        return cliplim

if value2 >= cliplimit():
    print('ABOVE Clip level - EU PO is required')
    return ('ABOVE Clip level')
else:
    print('value {} USD matches expected. BELOW Clip level'.format(value2))
    return ('BELOW clip level')

So far the error I get is related to the positional argument cliplim:

line 37, in <module>   f(input1, value1) 
line 23, in f
    if value2 >= cliplimit(): TypeError: cliplimit() missing 1 required positional argument: 'cliplim'

I hope it is not too confusing, but the juice is that I need to assign specific values to specific countries/currencies for clip limit. Thank you for any help!

2 Answers

Have you tried removing the parameter from your cliplimit function? You don't actually use the value passed in for the function.

The error you're getting on line 23 is because you're not passing a value to a function that is expecting one. Since your function doesn't need a value passed in you can remove the parameter and simply define it as def cliplimit()

Just to clarify, you do use the cliplim variable in the function, but not the value that's passed in. So you will need to define the variable and possibly set it to a default value.

Also, you have a return statement inside of an iteration so you could remove the loop as well. I don't think you're expecting it to evaluate more than once. I'm thinking you're probably better off passing in the country code to the function and removing the for country in f

One more thing... regarding the original f function... You don't want to do your conditional tests against your country list, you want to use the country parameter that's passed in so you'd be better off with conditionals like if country.upper() == "PL": - You can probably do without the list all together.

Irregardless of all of the above... if you're just looking to assign a cliplimit value to each country that you can then lookup and use when needed, then consider using a dictionary

cliplims = {"PL" : 20, "CZ" : 30} 
print (cliplims["CZ"])

Based on the comments from the first answer and your original code...

Would still need proper error handling

#Take in a country and a Value
#Do the conversion based on country
#Determine need for EU PO based on clip level

cliplims = {"PL" : 20, "CZ" : 30}

def convert(country, value):
    if country.upper() == "PL":
        return round(value / 3, 2)
    elif country.upper() == "CZ":
        return round(value / 20, 2)

def above_cliplim(country, value):
    return value >= cliplims[country]

my_country = str(input("Enter Country: "))
my_value = float(input("Enter Value: "))

converted_value = convert(my_country, my_value)
print('The conversion of', my_value, my_country, 'is:', converted_value, 'USD')

if above_cliplim(my_country, converted_value):
    print('ABOVE Clip level - EU PO is required')
else:
    print('value {} BELOW Clip level'.format(converted_value))
Related