def duration_in_mins(datum, city):
"""
Takes as input a dictionary containing info about a single trip (datum) and
its origin city (city) and returns the trip duration in units of minutes.
Remember that Washington is in terms of milliseconds while Chicago and NYC
are in terms of seconds.
HINT: The csv module reads in all of the data as strings, including numeric
values. You will need a function to convert the strings into an appropriate
numeric type when making your transformations.
see https://docs.python.org/3/library/functions.html
"""
datum_n = round(float(example_trips['NYC']['tripduration'])/60,4)
print(datum_n)
datum_c = round(float(example_trips['Chicago']['tripduration'])/60,4)
print(datum_c)
datum_w = round(float(example_trips['Washington']['Duration (ms)'])/60000,4)
print(datum_w)
duration={'NYC': datum_n,
'Chicago': datum_c,
'Washington': datum_w}
print(duration)
return duration
tests = {'NYC': 13.9833,
'Chicago': 15.4333,
'Washington': 7.1231}print(duration)
**for city in tests:
assert abs(duration_in_mins(example_trips[city], city) - tests[city]) < .001**
TypeError Traceback (most recent call last)
<ipython-input-10-90486a3cfc17> in <module>()
45
46 for city in tests:
---> 47 assert abs(duration_in_mins(example_trips[city], city) - tests[city]) < .001
TypeError: unsupported operand type(s) for -: 'dict' and 'float'
您正在比较dict
和float
。作为算术运算,不能将一个与另一个相反。
特别:
duration_in_mins(example_trips[city], city)
返回一个dict
对象。tests[city]
返回一个float
对象。我的猜测是你想要的:
duration_in_mins(example_trips[city], city)[city] - tests[city]