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 trial

Python

Robert Baucus
Robert Baucus
3,400 Points

Python: 'appointments.py' refactor need to figure out how to change a str type object into a tzinfo subclass

I am refactoring the "Times in Python" project "appointments.py" so that the user can convert his 'appointment time' into any selected timezone. I have the list of all timezones as a dictionary (pytz.all_timezones, enumerated and put into a dict) and then I access the value by a user input selected number as the key (e.g. 453 gets your 'Europe/Madrid').

The problem seems to be when I call up this tzinfo to spit out the users time with a new timezone the program says the timezone info called up is a str not a 'tzinfo subclass', like so:

When is your meeting? Please use MM/DD/YYYY HH:MM format. Enter Q to quit. : 08/22/2017 23:45
Pick a timezone by its number (1-593) or pick RANDOM : 453
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python36\learningPython\one_to_six_tz_treehouse2.py", line 54, in <module>
    output.append(utc_date.astimezone(all_timezones_dict[time_zone_choice]))
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'str'

The body of the program is the following:

# things to be added: 
# way for user to pick the timezone, with all timezones and RANDOM option

from datetime import datetime

import pytz
import sys
import random

all_timezones_dict = (dict(enumerate(pytz.all_timezones, start=1)))

''' #from previous version
OTHER_TIMEZONES = [
    pytz.timezone('US/Eastern'),
    pytz.timezone('Pacific/Auckland'),
    pytz.timezone('Asia/Calcutta'),
    pytz.timezone('UTC'),
    pytz.timezone('Europe/Paris'),
    pytz.timezone('Africa/Khartoum')
]
'''
fmt = '%Y-%m-%d %H:%M:%S %Z%z'



while True:
    input("Welcome to International Innernet Timezone Switcher. Take a look at our 593 timezones and remember the number!")
    input("Press any key to continue ! Or don't, see if I care...")
    for k, v in all_timezones_dict.items():
        print(k, v)

    date_input = input("When is your meeting? Please use MM/DD/YYYY HH:MM format. Enter Q to quit. : ")
    time_zone_choice = int(input("Pick a timezone by its number (1-593) or pick RANDOM : "))


    try:
        local_date = datetime.strptime(date_input, '%m/%d/%Y %H:%M')

    except ValueError:
        if date_input == 'q'.upper() or date_input == 'q':
            sys.exit()

        elif date_input == 'random' or 'RANDOM':
            time_zone_choice = random.randint(1, 593)

        else:
            print("{} doesn't seem to be a valid date & time.".format(date_input))

    else:
        local_date = pytz.timezone('US/Pacific').localize(local_date)
        utc_date = local_date.astimezone(pytz.utc)

        output = []
        output.append(utc_date.astimezone(all_timezones_dict[time_zone_choice]))
        for appointment in output:
            print(appointment.strftime(fmt))
        break

I hope that there is a way to change the string accessed by "all_timezones_dict[time_zone_choice] into a tzinfo subclass. Or maybe use replace() to add this info back manually? I am all turned around here!

RJB