Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDaniel Tompkins
5,591 Pointstimestamp.py trouble: 'float int not iterable'
Hi, I keep getting a <'float' not iterable> error when I run this code. However, I've gotten it to work in workspaces-- even after I rewrote it in a couple of different ways... I would think that with the <if len(time_stamp) == 1>, I should get a tuple with multiple values, so the "for statement" should be iterating through an index, right? HELP PLEASE!
# If you need help, look up datetime.datetime.fromtimestamp()
# Also, remember that you *will not* know how many timestamps
# are coming in.
import datetime
times = 3843764839.22
def timestamp_oldest(*time_stamp):
if len(time_stamp) == 1:
return datetime.datetime.fromtimestamp(time_stamp[0])
else:
for args in tuple(time_stamp):
time_list = list(args)
time_list.sort()
list_length = len(time_list) - 1
return datetime.datetime.fromtimestamp(time_list[list_length])
3 Answers
Umesh Ravji
42,386 PointsHi Daniel, there's a small issue with how you are using the for
loop:
for args in tuple(time_stamp): # tuple of all timestamps
time_list = list(args) # here you are trying to convert a single timestamp into a list
It's not the last item you want from the list, but the first. Taking from the code you have written what you want is essentially this:
def timestamp_oldest(*args):
time_list = list(args)
time_list.sort()
return datetime.datetime.fromtimestamp(time_list[0])
[Edit] Wrong variable :p
Daniel Tompkins
5,591 PointsThanks for the help. I got it eventually... but I'm still a little wary of tuple iterations and unpacking. I'll get a better grasp on it as my experience grows, I suppose.
Umesh Ravji
42,386 PointsWhen I first started programming, I think it took me about 3 months to understand that functions are able to accept parameters, and are able to return values :p
Daniel Tompkins
5,591 Pointshaha Thanks for the encouragement Umesh! Cool thing to hear from such an accomplished user!