2017-11-29 6 views
1

大文字と小文字の辞書を作成し、後でランダムな状態を選択して、の資本と_です。私が持っています州の首都とガバナーの辞書を作成する方法

私は状態が未定義であり、私は理由を理解できません。

+0

1. state_dict()関数でreturn文を使用します。 2.ステートメントchoose_state = random.choice(states_dict)をchoose_state = random.choice(states_dict()。keys())に置き換えます。 – Anup

+0

トレースバックを投稿してください。 – wwii

+0

[Pythonスコープとネームスペース](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces)関数内で* states *を作成しない*モジュール内で*作成する*。 – wwii

答えて

0

Update2: @schwobasegglは、ここではstates_dict()関数を使用する理由がないことを指摘しています。ちょうど

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. 

また、あなたはPEP8スタイルのガイドを勉強し、あなたの筋肉の記憶に今組み込む必要があります。


更新:

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) 

オリジナル: あなたのインデントが正しくありません。

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

州は、state_dict()関数の外にあります。

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

また、PEP8スタイルのガイドラインに従っていない可能性があります。そのためにコードを作成して圧縮している可能性があります。読書人のために、これはコードをフォーマットするためのより良い方法です:

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

私はそれが私のプログラムの中で好きです。申し訳ありませんが、私はここで間違って書きました – BarbaraJ

4

あなたはstatesのような静的辞書ともなし主な機能のための機能は必要ありません。これを多く簡略化することができます:

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 
0

辞書をこのような形式で作成してみませんか?

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

それはより多くのそのようなJSONデータのように流れます。

プラスここにあなたの質問では、

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

あなたはstatesのdictを返しません。あなたはrandom.choice(state_dict())でそれを使用する場合Ajax1234がstate_dictであなたのインデントを指摘@としてだから、random.choice(state_dict())

0
  1. states辞書を渡してしまうことはありませんオフになっていると、あなたはreturn states
  2. あなたの機能を追加する必要がstate_dictですが、あなたはしていますあなたは1を修正した場合random.choice(states_dict())

random.choice(states_dict)を切り替える必要がありstates_dict

  • を呼び出します3あなたは行くのが良いはずです。

  • +0

    どれがOPの問題を解決していますか? – wwii

    関連する問題