[Solved] I am always receiving an AttributeError


Hope this works for you.

code

import time
import datetime
import pytz
print('--'*62)
cont = 0
print('MENU'.center(115))
print('--'*62)
my_timezones = {}
l = list()  # MAIN LIST
l.append('0')
my_timezones['United Arab Emirates'] = pytz.country_timezones['AE'][0]
my_timezones['Canada'] = pytz.country_timezones['CA'][20]
my_timezones['South Korea'] = pytz.country_timezones['KR'][0]
my_timezones['United States'] = pytz.country_timezones['US'][17]
my_timezones['New Zeland'] = pytz.country_timezones['NZ'][0]
my_timezones['Norway'] = pytz.country_timezones['NO'][0]
my_timezones['Ireland'] = pytz.country_timezones['IE'][0]
my_timezones['Netherlands'] = pytz.country_timezones['NL'][0]
my_timezones['Switzerland'] = pytz.country_timezones['CH'][0]
l.append(my_timezones['United Arab Emirates']) 
l.append(my_timezones['Canada']) 
l.append(my_timezones['South Korea'])
l.append(my_timezones['United States'])
l.append(my_timezones['New Zeland']) 
l.append(my_timezones['Norway'])
l.append(my_timezones['Ireland']) 
l.append(my_timezones['Netherlands'])
l.append(my_timezones['Switzerland'])
print(l)
print('Cod   Country                      Timezone')
for c in my_timezones.keys():
    cont+=1
    print(f'{cont}', end='     ')
    print(f'{c}', end='')
    print(f'{my_timezones[c]}'.center(45))
print('--'*62)
while True:
    wich = int(input('Wich one? '))
    if wich == 0:
        break
    else:
        print(f'Local Time: {datetime.datetime.now()}')
        print(f'UTC time: {datetime.datetime.utcnow()}')
        try:
            baby = l[wich] 
            timezone = pytz.timezone(str(baby)) # convert to string value
            time = datetime.datetime.now(tz=timezone)
            print(f'{l[wich]} time: {time}')
        except IndexError:
            print('SORRY! WE DIDN´T FIND IT!')
for c in l:
    print(c)

output

----------------------------------------------------------------------------------------------------------------------------
                                                        MENU

----------------------------------------------------------------------------------------------------------------------------
['0', 'Asia/Dubai', 'America/Yellowknife', 'Asia/Seoul', 'America/Denver', 'Pacific/Auckland', 'Europe/Oslo', 'Europe/Dublin', 'Europe/Amsterdam', 'Europe/Zurich']
Cod   Country                      Timezone
1     United Arab Emirates                  Asia/Dubai
2     Canada             America/Yellowknife
3     South Korea                  Asia/Seoul
4     United States                America/Denver
5     New Zeland               Pacific/Auckland
6     Norway                 Europe/Oslo
7     Ireland                Europe/Dublin
8     Netherlands               Europe/Amsterdam
9     Switzerland                Europe/Zurich
----------------------------------------------------------------------------------------------------------------------------
Wich one? 1
Local Time: 2019-11-19 13:39:29.562076
UTC time: 2019-11-19 00:39:29.563094
Asia/Dubai time: 2019-11-19 04:39:29.564126+04:00
Wich one? 2
Local Time: 2019-11-19 13:39:31.849463
UTC time: 2019-11-19 00:39:31.849463
America/Yellowknife time: 2019-11-18 17:39:31.851457-07:00
Wich one? 3
Local Time: 2019-11-19 13:39:32.923009
UTC time: 2019-11-19 00:39:32.923009
Asia/Seoul time: 2019-11-19 09:39:32.924061+09:00
Wich one? 4
Local Time: 2019-11-19 13:39:33.977945
UTC time: 2019-11-19 00:39:33.978947
America/Denver time: 2019-11-18 17:39:33.979944-07:00
Wich one? 5
Local Time: 2019-11-19 13:39:34.849333
UTC time: 2019-11-19 00:39:34.850337
Pacific/Auckland time: 2019-11-19 13:39:34.851371+13:00
Wich one? 6
Local Time: 2019-11-19 13:39:35.570803
UTC time: 2019-11-19 00:39:35.570803
Europe/Oslo time: 2019-11-19 01:39:35.571802+01:00
Wich one? 7
Local Time: 2019-11-19 13:39:36.338700
UTC time: 2019-11-19 00:39:36.338700
Europe/Dublin time: 2019-11-19 00:39:36.341697+00:00
Wich one? 8
Local Time: 2019-11-19 13:39:37.234597
UTC time: 2019-11-19 00:39:37.234597
Europe/Amsterdam time: 2019-11-19 01:39:37.236625+01:00
Wich one? 9
Local Time: 2019-11-19 13:39:38.097607
UTC time: 2019-11-19 00:39:38.098611
Europe/Zurich time: 2019-11-19 01:39:38.099586+01:00
Wich one? 10
Local Time: 2019-11-19 13:39:43.267515
UTC time: 2019-11-19 00:39:43.267515
SORRY! WE DIDN´T FIND IT!
Wich one?

0

solved I am always receiving an AttributeError