Python 3.6.4 Real Estate Taxes # # Define a function calculate_property_tax # Gi
ID: 3904671 • Letter: P
Question
Python 3.6.4
Real Estate Taxes
#
# Define a function calculate_property_tax
# Given a house value, such as 400000, computes real estate taxes due
# using a rate of taxes: $6.25 per thousand (1000) dollars of value.
# Your function definition goes here
# Run the code to test your function.
##home_value = 300000 # $300,000
##property_tax = calculate_property_tax(home_value) # 1875.0
##print("home value", home_value, "owes $", property_tax, "for property taxes")
###
##house_price = 475000 # $475,000
##tax = calculate_property_tax(house_price) # 2968.75
##print("home value", house_price, "owes $", tax, "for property taxes")
###
## CALL a function
# Write one line of code to get the total of this list
# To get credit, your code must work even if the list had different values
# or a different number of numbers.
Explanation / Answer
def calculate_property_tax(value):
tax = 6.25*value/1000
return tax
Now assuming the list mentioned in the question is the list of input values for the function, the sum of the property taxes for the given values can be calculated by
reduce(lambda x,y:x+y,list(map(calculate_property_tax,given_list)))
Lets see the functions one-by-one
map() : The map function in python is used to apply a list of inputs into a function one at a time and get the output. Here it uses our function as the function to be used and the given_list is the list of input values
list() : This converts the result of the map function into a list
reduce() : The reduce function is used to perform computations on a list. We create an anonymous lambda function that calculates the sum of 2 values and we pass the list to this function.
PS : The reduce function is in the functools library. Use 'from functools import reduce' in the beginning for it to work. Also initialise the given_list with input values before calling it
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.