-1

So i am in class for Python 1. Not we have 4 lists, all lists are in strings. I need to request a state input from a user and then find the index of that state and print the congressional districts, the order that state joined the union and the capital of that state, now i can get the index of the state which matches the index of the other 3 lists but i cant seem to print the str of that index number. Any idea what im doing wrong i always get the following error.

    , line 225, in <module>
    print(joined.index())
AttributeError: 'int' object has no attribute 'index'

here is the full code i currently have

 state =[
'Alabama',
'Alaska',
'Arizona',
'Arkansas',
'California',
'Colorado',
'Connecticut',
'Delaware',
'Florida',
'Georgia',
'Hawaii',
'Idaho',
'Illinois',
'Indiana',
'Iowa',
'Kansas',
'Kentucky',
'Louisiana',
'Maine',
'Maryland',
'Massachusetts',
'Michigan',
'Minnesota',
'Mississippi',
'Missouri',
'Montana',
'Nebraska',
'Nevada',
'New Hampshire',
'New Jersey',
'New Mexico',
'New York',
'North Carolina',
'North Dakota',
'Ohio',
'Oklahoma',
'Oregon',
'Pennsylvania',
'Rhode Island',
'South Carolina',
'South Dakota',
'Tennessee',
'Texas',
'Utah',
'Vermont',
'Virginia',
'Washington',
'West Virginia',
'Wisconsin',
'Wyoming'
]

capital =[
'Montgomery',
'Juneau',
'Phoenix',
'Little Rock',
'Sacramento',
'Denver',
'Hartford',
'Dover',
'Tallahassee',
'Atlanta',
'Honolulu',
'Boise',
'Springfield',
'Indianapolis',
'Des Moines',
'Topeka',
'Frankfort',
'Baton Rouge',
'Augusta',
'Annapolis',
'Boston',
'Lansing',
'St. Paul',
'Jackson',
'Jefferson City',
'Helena',
'Lincoln',
'Carson City',
'Concord',
'Trenton',
'Santa Fe',
'Albany',
'Raleigh',
'Bismarck',
'Columbus',
'Oklahoma City',
'Salem',
'Harrisburg',
'Providence',
'Columbia',
'Pierre',
'Nashville',
'Austin',
'Salt Lake City',
'Montpelier',
'Richmond',
'Olympia',
'Charleston',
'Madison',
'Cheyenne'
]

districts =[
'7',
'1',
'8',
'4',
'53',
'7',
'5',
'1',
'25',
'13',
'2',
'2',
'19',
'9',
'5',
'4',
'6',
'7',
'2',
'8',
'10',
'15',
'8',
'4',
'9',
'1',
'3',
'3',
'2',
'13',
'3',
'29',
'13',
'1',
'18',
'5',
'5',
'19',
'2',
'6',
'1',
'9',
'32',
'3',
'1',
'11',
'9',
'3',
'8',
'1',
]

joined =[
'22',
'49',
'48',
'25',
'31',
'38',
'5',
'1',
'27',
'4',
'50',
'43',
'21',
'19',
'29',
'34',
'15',
'18',
'23',
'7',
'6',
'26',
'32',
'20',
'24',
'41',
'37',
'36',
'9',
'3',
'47',
'11',
'12',
'39',
'17',
'46',
'33',
'2',
'13',
'8',
'40',
'16',
'28',
'45',
'14',
'10',
'42',
'35',
'30',
'44'
]

user_state = input('Please enter the State you would like info about: ')
index = state.index(user_state)

joined = index

print(joined.index())
  • 1
    `joined = index` means that `joined` is now an integer, not the list you had previously defined. Also worth noting: avoid naming variables like `index` that shadow the built in method `index()`, that can cause you many problems – G. Anderson Jan 25 '22 at 23:23
  • 1
    Were you after `print(joined[index])`? You definitely don't want that `joined = index` line, as it loses the list that was assigned to `joined` and afterwards `joined` will just be the same value as `index` – Grismar Jan 25 '22 at 23:25
  • To print the index (the number), just `print(index)`. To print that item from other lists just `print(capital[index])` – Mark Jan 25 '22 at 23:26
  • So i ended up figuring it out. I was thinking the same thing in regards to the joined = index after running it through my mind on where in the index number was in the code after staring at this for three hours i finally focused more on the given sample code for the class. and ended up with this. – Matthew VanDruff Jan 25 '22 at 23:43
  • `user_state = input('Please enter the State you would like info about: ') list_index = state.index(user_state) joined_number = joined[list_index] districts_number = districts[list_index] capital_name = capital[list_index] print('The Capital of ' + user_state + ' is ' + capital_name + '. It has ' ` – Matthew VanDruff Jan 25 '22 at 23:45
  • Your question would probably be better received if the code was just a [mcve] :-) – Robert Feb 03 '22 at 22:24

1 Answers1

0

Consider using str.title and list.index:

STATE = [
    'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
    'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
    'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
    'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
    'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
    'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio',
    'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina',
    'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia',
    'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
]

CAPITAL = [
    'Montgomery', 'Juneau', 'Phoenix', 'Little Rock', 'Sacramento', 'Denver',
    'Hartford', 'Dover', 'Tallahassee', 'Atlanta', 'Honolulu', 'Boise',
    'Springfield', 'Indianapolis', 'Des Moines', 'Topeka', 'Frankfort',
    'Baton Rouge', 'Augusta', 'Annapolis', 'Boston', 'Lansing', 'St. Paul',
    'Jackson', 'Jefferson City', 'Helena', 'Lincoln', 'Carson City', 'Concord',
    'Trenton', 'Santa Fe', 'Albany', 'Raleigh', 'Bismarck', 'Columbus',
    'Oklahoma City', 'Salem', 'Harrisburg', 'Providence', 'Columbia', 'Pierre',
    'Nashville', 'Austin', 'Salt Lake City', 'Montpelier', 'Richmond',
    'Olympia', 'Charleston', 'Madison', 'Cheyenne'
]

DISTRICTS = [
    '7', '1', '8', '4', '53', '7', '5', '1', '25', '13', '2', '2', '19', '9',
    '5', '4', '6', '7', '2', '8', '10', '15', '8', '4', '9', '1', '3', '3',
    '2', '13', '3', '29', '13', '1', '18', '5', '5', '19', '2', '6', '1', '9',
    '32', '3', '1', '11', '9', '3', '8', '1'
]

JOINED = [
    '22', '49', '48', '25', '31', '38', '5', '1', '27', '4', '50', '43', '21',
    '19', '29', '34', '15', '18', '23', '7', '6', '26', '32', '20', '24', '41',
    '37', '36', '9', '3', '47', '11', '12', '39', '17', '46', '33', '2', '13',
    '8', '40', '16', '28', '45', '14', '10', '42', '35', '30', '44'
]

# Modifed from https://stackoverflow.com/a/52045942/6328256
def get_joined_with_suffix(joined: str) -> str:
    suffix = ['th', 'st', 'nd', 'rd']
    joined = int(joined)
    if joined % 10 in {1, 2, 3} and joined not in {11, 12, 13}:
        return f'{joined}{suffix[joined % 10]}'
    else:
        return f'{joined}{suffix[0]}'


def main() -> None:
    user_state = input('Please enter the state you would like info about: ').title()
    user_state_index = STATE.index(user_state)
    user_state_districts = DISTRICTS[user_state_index]
    user_state_capital = CAPITAL[user_state_index]
    user_state_joined = JOINED[user_state_index]
    user_state_joined_with_suffix = get_joined_with_suffix(user_state_joined)
    district_or_districts = 'district' if user_state_districts == '1' else 'districts'
    print(f'{user_state} has {user_state_districts} {district_or_districts}')
    print(f'{user_state} was the {user_state_joined_with_suffix} state to join the union')
    print(f'{user_state}\'s capital is {user_state_capital}')


if __name__ == '__main__':
    main()

Example Usage:

Please enter the state you would like info about: new York
New York has 29 districts
New York was the 11th state to join the union
New York's capital is Albany
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40