Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#Write a function called clean _ data. clean _ data takes one #parameter, a dictionary. The dictionary represents the #observed rainfall in inches on a
#Write a function called cleandata. cleandata takes one
#parameter, a dictionary. The dictionary represents the
#observed rainfall in inches on a particular calendar day
#at a particular location. However, the data has some
#errors.
#
#cleandata should delete any keyvalue pair where the value
#has any of the following issues:
#
# the type is not an integer or a float. Even if the value
# is a string that could be converted to an integer eg
# it should be deleted.
# the value is less than : it's impossible to have a
# negative rainfall number, so this must be bad data.
# the value is greater than : the world record for
# rainfall in a day was inches
#
#Return the dictionary when you're done making your changes.
#
#Remember, the keyword del deletes items from lists
#and dictionaries. For example, to remove the key "key!" from
#the dictionary mydict, you would write: del mydictkey
#Or if the key was the variable mykey, you would write:
#del mydictmykey
#
#Hint: If you try to delete items from the dictionary while
#looping through the dictionary, you'll run into problems!
#We should never change the number if items in a list or
#dictionary while looping through those items. Think about
#what you could do to keep track of which keys should be
#deleted so you can delete them after the loop is done.
#
#Hint : To check if a variable is an integer, use
#typethevariable int. To check if a variable is a float,
#use typethevariable float.
#Write your function here!
def cleandataadict:
badlist
for value in adict.values:
if typevalueint or typevaluefloat:
if value or value:
badlist.appendvalue
else:
pass
else:
badlist.appendvalue
for badname in badlist:
del adictbadname
return adict
#Below are some lines of code that will test your function.
#You can change the value of the variables to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print although the order of the keys may vary:
#: : : :
rainfall : : :
: : :
:
printcleandatarainfall
My function is under the part of code where it says #Write your function here" I keep getting a Key Error. what is wrong?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started