我正在制作一个简单的程序,您可以在其中查看,添加或删除不同人的礼物。 我试图从另一个函数访问和编辑名为people {}的字典中的列表。 我该怎么办?
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))
答案 0 :(得分: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)
添加礼物
删除有效礼物
删除无效礼物