BarbaraJ
BarbaraJ

Reputation: 21

How to create a dictionary of capitals and governors of states

I want to create a dictionary of the capitals and governors and then later choose a random state to ask the user what the capital of _ is or who the governor of _ is. I have

import random

def state_dict():
    states={"Alabama":["montgomery","kay ivey"],"Alaska":["Juneau","Bill 
    Wallker"]}

def main():
    question_list=["What is the capital of","Who is the governor of"]
    choose_question=random.choice(question_list)
    if choose_question=="What is the capital of":
        choose_state= random.choice(states_dict)
        print("What is the capital of",choose_state)

    elif choose_question=="Who is the governor of":
        choose_state_2= random.choice(states_dict)
        print("Who is the governor of", choose_state_2)

main() 

I get an error that states is undefined and I am unable to figure out why.

Upvotes: 1

Views: 2232

Answers (4)

rer
rer

Reputation: 1268

  1. As @Ajax1234 noted your indentation in state_dict is off and you need to add return states
  2. Your function is state_dict but you're calling states_dict
  3. You need to switch random.choice(states_dict) to random.choice(states_dict())

If you fix 1 & 3 you should be good to go.

Upvotes: 0

kchawla-pi
kchawla-pi

Reputation: 449

Update2: As @schwobaseggl has pointed out there's no reason to use the function states_dict() here. Just do

def main():
    states_dict = {
        "Alabama": ["montgomery", "kay ivey"],
        "Alaska": ["Juneau", "Bill Wallker"],
        }
    question_list = ["What is the capital of","Who is the governor of"]
    choose_question = random.choice(question_list)
    if choose_question == "What is the capital of":
        choose_state = random.choice(list(states_dict.keys()))  # this will give you a random key from the dict.

Also, you should study the PEP8 style guide and incorporate it in your muscle memory now.


Update:

def state_dict():
    states={"Alabama":["montgomery","kay ivey"],"Alaska":["Juneau","Bill 
    Wallker"]}
    return states  # returned states dict.

def main():
    question_list=["What is the capital of","Who is the governor of"]
    choose_question=random.choice(question_list)
    if choose_question=="What is the capital of":
        choose_state= random.choice(states_dict())  # added ()
        print("What is the capital of",choose_state)

Original: You indentation is incorrect.

def state_dict():
states={"Alabama":["montgomery","kay ivey"],"Alaska":["Juneau","Bill 
Wallker"]}

states is outside the function state_dict(). Make it:

def state_dict():
    states={"Alabama":["montgomery","kay ivey"],"Alaska":["Juneau","Bill 
Wallker"]}

Additionally, you haven't followed the PEP8 style guidelines, maybe you did and compacted the code for SO. For those reading, this is a better way to format the code:

def state_dict():
    states = {
        "Alabama": ["montgomery", "kay ivey"],
        "Alaska": ["Juneau", "Bill Wallker"],
        }

Upvotes: 0

user2390182
user2390182

Reputation: 73480

You don't need a function for a static dict like states and also no main function. You can simplify this a lot:

states = {
    "Alabama": ["Montgomery", "Kay Ivey"],
    "Alaska": ["Juneau", "Bill Wallker"]
}
question_list = ["What is the capital of", "Who is the governor of"]

choose_state= random.choice(states)
choose_question = random.randint(0, 1)  # just an index that can be reused
answer = input(question_list[choose_question], choose_state)
if answer == states[choose_state][choose_question]:
    # yay
else:
    # aww

Upvotes: 4

cmpgamer
cmpgamer

Reputation: 352

Why not make your dictionary in this type of format?

states = {'Alabama': {'capital': 'Montgomery', 'governor':'Kay Ivey'....}

It flows more like Json data that way.

Plus here in your question,

def state_dict(): states={"Alabama":["montgomery","kay ivey"],"Alaska":["Juneau","Bill Wallker"]}

You don't return the states dict. So when you use it in random.choice(state_dict()) you never end up passing the states dict to random.choice(state_dict())

Upvotes: 0

Related Questions