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!
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 trial

Vinod Purushothaman
2,970 PointsValueError is not handled properly by python in Timezonapalooza tutorial.
What is wrong with error handling in this code? while testing the script python is not handling the ValueError.
# import datetime and pytz library
from datetime import datetime
import pytz
OTHER_TIMEZONES = [
pytz.timezone('US/Eastern'),
pytz.timezone('Pacific/Auckland'),
pytz.timezone('Asia/Calcutta'),
pytz.timezone('Europe/Paris'),
pytz.timezone('Africa/Khartoum')
]
# time & date output format
fmt = "%Y-%m-%d %H:%M:%S %Z%z"
# get meeting date & time from user
while True:
date_input = input("When is your meeting? Please use MM/DD/YYYY HH:MM format. ")
try:
local_date = datetime.strptime(date_input, '%m/%d/%Y %H:%M')
except ValueError:
print("{} doesn't seem to be a valid date & time.".format(date_input))
else:
# localize date & time
local_date = pytz.timezone('Africa/Johannesburg').localize(local_date)
# covert date & time to utc
utc_date = local_date.astimezone(pytz.utc)
# print meeting date & time in 6 different time zones.
output = []
for timezone in OTHER_TIMEZONES:
output.append(utc_date.astimezone(timezone))
for appointment in output:
print(appointment.strftime(fmt))
break
1 Answer

Andrew Mlamba
15,642 PointsThis section of code should all be under the else condtion. In your case i think this part of code cannot be reached.
# print meeting date & time in 6 different time zones.
output = []
for timezone in OTHER_TIMEZONES:
output.append(utc_date.astimezone(timezone))
for appointment in output:
print(appointment.strftime(fmt))
break