lundi 7 novembre 2016

Variable set in 'if statement' can't be printed outside of it in Python

When I assign a variable inside the if statement it doesn't print out at the bottom:

#!/usr/bin/env python

import datetime
date = datetime.datetime.today().weekday()

#0 monday, 1 tuesday, 2 wednesday, 3 thursday, 4 friday, 5 saturday, 6 su$

if (date == 1 or date == 2): #tuesday wednesday
        location = 'Baltimore'
if (date == 3): #thursday
        location = 'DC'
if (date == 4):
        location = 'Johns Hopkins Cath/Baltimore'

The error I get is:

Traceback (most recent call last):
  File "./ifwhile.py", line 13, in <module>
    print location
NameError: name 'location' is not defined

Aucun commentaire:

Enregistrer un commentaire