简体   繁体   中英

I want to use a specific value from a dictionary in a function - Python

So, I'm trying to build a function that takes a state and income. I've created a dictionary of every state in the US, next to the value as a float of the tax %. My problem is that I don't know how to compare the state that the function takes, and then look for that specific state value, in this case, that would be the tax rate and use it in the function to make the calculations.

Should look something like this I guess, maybe with a for loop?

states = {'Alabama': 0.04, 'Alaska': 0, 'Arizona': 0.056}

def taxes(state,income):

    tax_rate = 0
    if state == 'Alabama':
        tax_rate = 0.04
    income_with_taxes = income*tax_rate
    print(income_with_taxes)6

You just need to reference the dictionary using state as the key:

tax_rate = states[state]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM