-1

This is very hard to explain in words but here is a screenshot Basically I need addy.py to generate an state abbreviation and states.py contains all the list for the full name of the state or province so in addy.py the abbreviation is obtain through ab = fake.province_abbr() and now I want it to get the province full name by grabbing from states.py by doing this province = (states.ab) but I do not know how you should use ab on states.py to make it use or find the abbreviation set by addy.py States addy

Proposen
  • 1
  • 2
  • 2
    [Discourage screenshots of code, data, and/or errors](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – wwii Jan 26 '20 at 03:57
  • Well the first thing that pops up is the fact that you have a `.` after `states` in `province = (states. + ab)`. – OakenDuck Jan 26 '20 at 04:03

1 Answers1

1

Sounds like you're trying to access a variable in states (i.e. variable AB) from a string 'AB'. This answer should do the trick in this case.

# states.py
AB = ('Alberta')
BC = ('British Columbia')
# etc.
# addy.py
import states

abbr = 'AB' # get from "Faker" or wherever else
print(getattr(states, abbr))

This prints "Alberta"

Patrick Fay
  • 538
  • 3
  • 15
  • Traceback (most recent call last): File "C:/Users/WD/PycharmProjects/paypal/start.py", line 1, in import addy File "C:\Users\WD\PycharmProjects\paypal\addy.py", line 19, in print(getattr(states, ab)) AttributeError: module 'states' has no attribute 'YT' Process finished with exit code 1 – Proposen Jan 26 '20 at 04:37
  • import states import random import string from faker import Faker from random import randrange fake = Faker('en_CA') eno = randrange(0, 10) ab = fake.province_abbr() street = fake.street_address() post1 = fake.postalcode_in_province(province_abbr= ab) city = fake.city() pn = fake.phone_number() fname = fake.first_name() sname = fake.last_name() email = fname + sname + '@mail.com' print(getattr(states, ab)) print (email) name = random.choice(open('name.txt').readlines()) here is my new code – Proposen Jan 26 '20 at 04:37
  • Your states.py doesn't have every territory, 'YT' (Yukon Territory) is missing from states.py. You are also missing Nunavut ('NU'), Northwest Territories ('NT'), and Prince Edward Island ('PEI'). Add those and that should fix your issue I believe. – Patrick Fay Jan 26 '20 at 04:44
  • 1
    Yes I just noticed it 5 mins ago got it to work thanks for the answer – Proposen Jan 26 '20 at 04:47