Define a function that takes a dictionary as an argument and returns the same dictionary but with different values | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Define a function that takes a dictionary as an argument and returns the same dictionary but with different values

Defining a function that takes a dictionary as an argument.

10th May 2017, 4:46 PM
trucktar
trucktar - avatar
3 ответов
+ 5
# define your tax var as global to avoid compute them each time you call the function taxblock_1 = (1000 - 0) * 0.00 taxblock_2 = (10000 - 1000) * 0.10 taxblock_3 = (20200 - 10000) * 0.15 taxblock_4 = (30750 - 20200) * 0.20 taxblock_5 = (50000 - 30750) * 0.25 def calculate_tax(dict): result = {} # a new dict to be returned as result # make a loop over the dict key,value pair for key, value in dict.items(): # iteritems() in Python2 if value < 1001: # more efficient than "in range(0, 1001)" result[key] = (value) * 0.00 # you can also test "1000 < value < 10001", but with 'elif' you already know that value is > 1000 because it's not < 10001 elif value < 10001: # in range(1001, 10001): result[key] = ((value - 1000) * 0.10) + taxblock_1 elif value < 20201: # in range(10001, 20201): result[key] = ((value - 10000) * 0.15) + taxblock_2 + taxblock_1 elif value < 30751: # in range(20201, 30751): result[key] = ((value - 20200) * 0.20) + taxblock_3 + taxblock_2 + taxblock_1 elif value < 50001: # in range(30751, 50001): result[key] = ((value - 30750) * 0.25) + taxblock_4 + taxblock_3 + taxblock_2 + taxblock_1 else: # "elif value > 50000" is unnecessary :P result[key] = ((value - 50000) * 0.30) + taxblock_5 + taxblock_4 + taxblock_3 + taxblock_2 + taxblock_1 return result d = { 'test': 3476, 'attempt':42, 'last':42005 } print(calculate_tax(d))
11th May 2017, 3:02 AM
visph
visph - avatar
+ 4
You didn't specify what the function has to do and how it is to alter the dictionary values, so here, take a look at a function dividing the values by 2: def diff_dict(d): for i in d.keys(): d[i] //= 2 return d sample_dict = {1: 4, 2: 8, 3: 12, 4: 16} print(diff_dict(sample_dict)) >>> {1: 2, 2: 4, 3: 6, 4: 8}
10th May 2017, 4:58 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
The question box doesn't allow for more than 128 characters, otherwise I'd have posted the question properly but here it goes: Define a Python function that takes as an argument, a dictionary containing key-value pairs of people's names as the keys and their yearly incomes as the values The function should return a dictionary containing key-value pairs of the same people's names and their yearly tax bill as the values. Here's a function I defined that only takes a value and calculates the tax bill to be charged: def calculate_tax(dict): taxblock_1 = (1000 - 0) * 0.00 taxblock_2 = (10000 - 1000) * 0.10 taxblock_3 = (20200 - 10000) * 0.15 taxblock_4 = (30750 - 20200) * 0.20 taxblock_5 = (50000 - 30750) * 0.25 if i in range(0, 1001): result_1 = (dict) * 0.00 return result_1 elif i in range(1001, 10001): result_2 = ((dict - 1000) * 0.10) + taxblock_1 return result_2 elif i in range(10001, 20201): result_3 = ((dict - 10000) * 0.15) + taxblock_2 + taxblock_1 return result_3 elif i in range(20201, 30751): result_4 = ((dict - 20200) * 0.20) + taxblock_3 + taxblock_2 + taxblock_1 return result_4 elif i in range(30751, 50001): result_5 = ((dict - 30750) * 0.25) + taxblock_4 + taxblock_3 + taxblock_2 + taxblock_1 return result_5 elif i > 50000: result_6 = ((dict - 50000) * 0.30) + taxblock_5 + taxblock_4 + taxblock_3 + taxblock_2 + taxblock_1 return result_6
10th May 2017, 6:08 PM
trucktar
trucktar - avatar