2017-12-17 10 views
-1

다른 사람들에게 선물을 보거나 추가하거나 제거 할 수있는 간단한 프로그램을 만들고 있습니다. 다른 함수에서 people {}이라는 사전에있는 목록에 액세스하고 편집하려고합니다. 어떻게해야합니까?Python : 다른 함수로 사전에있는 목록에 액세스하기

def people(): 

    people = {"Alan":["skate","computer"], 
       "John":["sweater","socks"]} 

    print("""Select one option: 

    1. Alan 
    2. John""") 

    gifts(people) 

def gifts(people): 

    person = input() 

    if person == "1": 
     print("The gifts of Alan are: %s" % people["Alan"]) 
     return person 
     gift_options() 

    elif person == "2": 
     print("The gifts of John are: %s" % people["John"]) 
     return person 
     gift_options() 
    else: 
     people() 

def gift_manage(person): 

    print ("""Select an option: 

    1. Add gift 
    2. Remove gift 
    3. Return to list of availible people""") 

    option = input() 

    if option == "1": 
     print ("What gift would you like to add to %s? " % person) 
     new_gift = input() 
     people[person].append(str(new_gift)) 
+1

잘하고있는 것처럼 보입니다. 오류 메시지가 나타나면 질문을 전체 추적의 텍스트로 업데이트해야합니다. – quamrana

+0

@ babygame0ver 제안 된 편집에 관해서, 나는 OP가 Python3로 작성하고 있음을 확신합니다. 코드에 오래된 Python 2.7 구문을 추가하지 마십시오. 'raw_input'은 Python3에서 단지'input'입니다. – m00am

+0

예, 변수 이름에 문제가있어서 stackoverflow에 잘못된 문자> = 6 인 경우 게시물을 편집해야한다는 규칙이 있습니다. – babygame0ver

답변

1
def people_gifts(): 

    people = {"Alan":["skate","computer"], 
       "John":["sweater","socks"]} 

    print("""Select one option: 

    1. Alan 
    2. John""") 

    #gifts(people) 
    return people 

def gifts(people): 

    person = input() 

    if person == "1": 
     print("The gifts of Alan are: %s" % people["Alan"]) 
     return person 

    elif person == "2": 
     print("The gifts of John are: %s" % people["John"]) 
     return person 
    else: 
     people_gifts() 

def gift_manage(person,people): 
    print("\n\n") 
    print(person,"has",people[person]) 
    print ("""Select an option: 

    1. Add gift 
    2. Remove gift 
    3. Return to list of availible people 
    4. Quit""") 

    option = input() 

    if option == "1": 
     print ("What gift would you like to add to %s? " % person) 
     new_gift = input() 
     people[person].append(str(new_gift)) 
     print ("gift added",people[person]) 

    elif option == "2": 
     print ("What gift would you like to remove to %s? " % person) 
     remove_gift = input() 
     if(remove_gift in people[person]): 

      people[person].remove(str(remove_gift)) 
      print ("gift removed",people[person]) 
     else: 
      print ("Sorry gift does not exist") 

    elif option == "3": 
     print ("Available people : ",list(people.keys())) 

    elif option == "4": 
     print ("Exiting...") 
     return people 

    else: 
     print ("Try again") 
     gift_manage(person,people) 

    print("Do you want to try again? Press 1") 
    choice=input() 
    if(choice=="1"): 
     gift_manage(person,people) 
    return people 


people=people_gifts() 
gifts(people) 
gift_manage("Alan",people) 

유효하지 않은 선물을 제거 유효한 선물

Removing a valid gift

을 제거 선물

Adding a gift

추가

Removing invalid gift