자본금 및 총재 사전을 작성한 다음 임의의 국가를 선택하여 자본이 인 사람 또는 누가 입니다. 내가 가지고있다자본금 및 국가 총재 사전 작성 방법
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()
나는 상태가 정의되지 않은 이유를 알 수 없다는 오류가 발생합니다.
1. state_dict() 함수에서 return 문을 사용하십시오. 2. 명령문 choose_state = random.choice (states_dict)를 choose_state = random.choice (states_dict(). keys())로 바꿉니다. – Anup
Traceback을 게시하십시오. – wwii
[Python 범위 및 이름 공간] (https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces). 함수에'states'를 만들지 마라 * 모듈에 * states *를 만들지 마라. – wwii