2017-11-29 1 views
1

자본금 및 총재 사전을 작성한 다음 임의의 국가를 선택하여 자본이 인 사람 또는 누가 입니다. 내가 가지고있다자본금 및 국가 총재 사전 작성 방법

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() 

나는 상태가 정의되지 않은 이유를 알 수 없다는 오류가 발생합니다.

+0

1. state_dict() 함수에서 return 문을 사용하십시오. 2. 명령문 choose_state = random.choice (states_dict)를 choose_state = random.choice (states_dict(). keys())로 바꿉니다. – Anup

+0

Traceback을 게시하십시오. – wwii

+0

[Python 범위 및 이름 공간] (https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces). 함수에'states'를 만들지 마라 * 모듈에 * 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() 함수 밖에 있습니다. 확인하십시오 :

또한, 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 DICT을 통과 결국 결코 당신은 당신의 기능을 state_dict입니다 return states
  2. 를 추가해야하지만 당신은있어
  3. states_dict 호출 당신은 당신이 1,763를 해결하는 경우 random.choice(states_dict())

random.choice(states_dict)을 전환해야3 당신은 잘 가야한다.

+0

어느 것이 OP 문제를 해결 하나? – wwii