1

I want to use form_sate_data which is a string str(), to reference same named local variable inside the is_valid_phone function, you can see in print function.

form_state_data will always be two character short code for states that also exists as local variable containing list of postal code as integer in function _is_valid_phone .

form_state_data = 'AL'
form_phone_data_sliced = 205

def is_valid_phone(form_state_data, form_phone_data_sliced):
    # Some codes are not correct.
  AL = [205, 251, 256, 334, 938]
  AK = [907]
  AZ = [480, 520, 602, 623, 928]
  AR = [479, 501, 870]
  CA = [209, 213, 279, 310, 323, 408, 415, 424, 442, 510, 530, 559, 562, 619, 626, 628, 650, 657, 661, 669, 707, 714, 747, 760, 805, 818, 820, 831, 858, 909, 916, 925, 949, 951]
  CO = [303, 719, 720, 970]
  CT = [203, 475, 860, 959]
  DE = [302]
  DC = [202]
  FL = [239, 305, 321, 352, 386, 407, 561, 727, 754, 772, 786, 813, 850, 863, 904, 941, 954]
  GA = [229, 404, 470, 478, 678, 706, 762, 770, 912]
  HI = [808]
  ID = [208, 986]
  IL = [217, 224, 309, 312, 331, 618, 630, 708, 773, 779, 815, 847, 872]
  IN = [219, 260, 317, 463, 574, 765, 812, 930]
  IA = [319, 515, 563, 641, 712]
  KS = [316, 620, 785, 913]
  KY = [270, 364, 502, 606, 859]
  LA = [225, 318, 337, 504, 985]
  ME = [207]
  MT = [339, 351, 413, 508, 617, 774, 781, 857, 978]
  NE = [308, 402, 531]
  NV = [702, 725, 775]
  NH = [603]
  NJ = [201, 551, 609, 640, 732, 848, 856, 862, 908, 973]
  NM = [505, 575]
  NY = [212, 315, 332, 347, 516, 518, 585, 607, 631, 646, 680, 716, 718, 838, 845, 914, 917, 929, 934]
  NC = [252, 336, 704, 743, 828, 910, 919, 980, 984]
  ND = [701]
  OH = [216, 220, 234, 330, 380, 419, 440, 513, 567, 614, 740, 937]
  OK = [405, 539, 580, 918]
  OR = [458, 503, 541, 971]
  MD = [240, 301, 410, 443, 667]
  MA = [218, 320, 507, 612, 651, 763, 952]
  MI = [228, 601, 662, 769]
  MN = [218, 320, 507, 612, 651, 763, 952]
  MS = [314, 417, 573, 636, 660, 816]
  MO = [406]
  PA = [215, 223, 267, 272, 412, 445, 484, 570, 610, 717, 724, 814, 878]
  RI = [401]
  SC = [803, 843, 854, 864]
  SD = [605]
  TN = [423, 615, 629, 731, 865, 901, 931]
  TX = [210, 214, 254, 281, 325, 346, 361, 409, 430, 432, 469, 512, 682, 713, 726, 737, 806, 817, 830, 832, 903, 915, 936, 940, 956, 972, 979]
  UT = [385, 435, 801]
  VT = [802]
  VA = [276, 434, 540, 571, 703, 757, 804]
  WA = [206, 253, 360, 425, 509, 564]
  WV = [304, 681]
  WI = [262, 414, 534, 608, 715, 920]
  WY = [307]
  print(form_phone_data_sliced in form_state_data)
is_valid_phone(form_state_data,form_phone_data_sliced)
RJT
  • 59
  • 7
  • Instead of creating all these variables, it would be better to store them all in a `dict`, with the variable names as the keys. – Tom Karzes Apr 08 '20 at 13:08
  • 2
    Instead of variables, why not put these in a `dict`? `{'AL': [205,...], 'AK': [907], ...}` – Cory Kramer Apr 08 '20 at 13:08
  • The suggestions of TomKarzes and CoryKramer are programmatical and maintainable. However, if you insist on reaching them via a string, see [this](https://stackoverflow.com/a/9437799/2926992). – Eray Erdin Apr 08 '20 at 13:12
  • Does this answer your question? [How to get the value of a variable given its name in a string?](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string) – Eray Erdin Apr 08 '20 at 13:12

3 Answers3

2

You should use a dictionary to store the state codes, below is a example of how to achieve this.

states = {
  'AL': [205, 251, 256, 334, 938],
  'AK': [907],
  'AZ': [480, 520, 602, 623, 928],
  'AR': [479, 501, 870],
  'CA': [209, 213, 279, 310, 323, 408, 415, 424, 442, 510, 530, 559, 562, 619, 626, 628, 650, 657, 661, 669, 707, 714, 747, 760, 805, 818, 820, 831, 858, 909, 916, 925, 949, 951],
  'CO': [303, 719, 720, 970],
  'CT': [203, 475, 860, 959],
  'DE': [302],
  'DC': [202],
  'FL': [239, 305, 321, 352, 386, 407, 561, 727, 754, 772, 786, 813, 850, 863, 904, 941, 954],
  'GA': [229, 404, 470, 478, 678, 706, 762, 770, 912],
  'HI': [808],
  'ID': [208, 986],
  'IL': [217, 224, 309, 312, 331, 618, 630, 708, 773, 779, 815, 847, 872],
  'IN': [219, 260, 317, 463, 574, 765, 812, 930],
  'IA': [319, 515, 563, 641, 712],
  'KS': [316, 620, 785, 913],
  'KY': [270, 364, 502, 606, 859],
  'LA': [225, 318, 337, 504, 985],
  'ME': [207],
  'MT': [339, 351, 413, 508, 617, 774, 781, 857, 978],
  'NE': [308, 402, 531],
  'NV': [702, 725, 775],
  'NH': [603],
  'NJ': [201, 551, 609, 640, 732, 848, 856, 862, 908, 973],
  'NM': [505, 575],
  'NY': [212, 315, 332, 347, 516, 518, 585, 607, 631, 646, 680, 716, 718, 838, 845, 914, 917, 929, 934],
  'NC': [252, 336, 704, 743, 828, 910, 919, 980, 984],
  'ND': [701],
  'OH': [216, 220, 234, 330, 380, 419, 440, 513, 567, 614, 740, 937],
  'OK': [405, 539, 580, 918],
  'OR': [458, 503, 541, 971],
  'MD': [240, 301, 410, 443, 667],
  'MA': [218, 320, 507, 612, 651, 763, 952],
  'MI': [228, 601, 662, 769],
  'MN': [218, 320, 507, 612, 651, 763, 952],
  'MS': [314, 417, 573, 636, 660, 816],
  'MO': [406],
  'PA': [215, 223, 267, 272, 412, 445, 484, 570, 610, 717, 724, 814, 878],
  'RI': [401],
  'SC': [803, 843, 854, 864],
  'SD': [605],
  'TN': [423, 615, 629, 731, 865, 901, 931],
  'TX': [210, 214, 254, 281, 325, 346, 361, 409, 430, 432, 469, 512, 682, 713, 726, 737, 806, 817, 830, 832, 903, 915, 936, 940, 956, 972, 979],
  'UT': [385, 435, 801],
  'VT': [802],
  'VA': [276, 434, 540, 571, 703, 757, 804],
  'WA': [206, 253, 360, 425, 509, 564],
  'WV': [304, 681],
  'WI': [262, 414, 534, 608, 715, 920],
  'WY': [307],
}

is_valid_phone = lambda state, code : code in states[state]

print(is_valid_phone('AL', 205))
print(is_valid_phone('AL', 2000005))
Philip Rollins
  • 1,271
  • 8
  • 19
1

If you really want to assign these to variables (bad practice), instead of making you can change function for class, and then access all variables by calling vars() on the class.

class Phone:
    AL = [1,2]
p = phone()
print(vars(p)['AL'])

vars() takes an object and outputs a dict of all objects inside it, accessible with strings.

Gabriel Petersson
  • 8,434
  • 4
  • 32
  • 41
0

What you want to do like people in the comments have said is make all of the states into a dictionary and then use this code:

   if form_phone_data_sliced in states[form_state_code]:
       return True
Jack Adee
  • 73
  • 1
  • 1
  • 6