问题:如果我向一个用户输入项目,它会保存在每个现有用户中,即使我稍后会创建一个新用户 我试图将列表保存到用户的dictonary 其中包括作为键的用户名,值将是列表, 列表更改后保存在最后,因为我有可以执行的功能 追加并删除项目后,用户将其列表删除后,退出并返回登录菜单。但我希望如果用户将登录您仍然可以改变相同的列表并再次保存并打开一个新列表。如果你有更好的建议,请告诉我。 非常感谢 对不起我的英文,评论不准确 代码:
WATCH_LIST = 1
WATCH_HOW_MANY_ITEMS_IN_LIST = 2
CHECK_IF_ITEM_IS_IN_LIST = 3
SHOW_THE_NUMBER_OF_TIMES_ITEM_IS_IN_THE_LIST = 4
DELETE_ITEM = 5
ADD_NEW_ITEM = 6
WATCH_INVALID_ITEMS = 7
TOTAL_SUM = 8
LOGOUT = 9
EXIT = 10
LOGIN = 1
REGISTER = 2
ENTER = '\n'
STOP = "0"
MINIMUM_LENGTH = 3
DELETE_ALL_ITEMS = 1
DELETE_ONE_ITEM = 2
def get_input(list):
'''
Function gets input from user to the dictionary.
input: dictionary - grocery list
output: none'''
item = raw_input("""Please enter items, enter 0 to stop: """)
list[item] = 1
while(item != STOP):
print list, " 1"
item = raw_input("")
if(item in list):
value = list[item]
list[item] = value + 1
else:
list[item] = 1
del list[item]
return None
def print_menu():
'''
Function prints the main menu
input: none
output: none'''
print """
Main Menu:
1. Watch grocery list.
2. Watch how many items are in the list.
3. Check if item is in the list.
4. Watch how many times item is appeared in the list.
5. Delete items from the list.
6. Add item to the list.
7. Watch all of the invalid items.
8. Watch the total sum of your items
9. logout
10. Exit
"""
return None
def print_delete_menu():
'''
Function prints all of the delete options you can do.
input: none
output: none'''
print """
Delete Menu:
1. Delete item by name (all of the them).
2. Delete item by name (only one of them).
"""
def print_num_of_times_item_is_listed(list, requested_item):
'''
Function gets item's name from the user and
returns the number of times it shows in the list.
input:
*list - grocery list
*requested item - string - item's name.
output: counter - int - the number of times the item is listed.
'''
item_in_list = False
for item in list.keys():
if(item == requested_item):
print "The item appears", list[item], "times in the list"
item_in_list = True
if(not item_in_list):
print "item isn't listed"
return None
def add_item_to_the_list(list):
'''
Function gets input from user to the dictionary.
input: dictionary - grocery list
output: none'''
item = raw_input("""Please enter item """)
if(item in list):
value = list[item]
list[item] = value + 1
else:
list[item] = 1
return None
def print_invalid_items(list):
'''
Function prints invalid items, which are items with non alphabetic chars or items with less than 3 chars.
input: list - grocery list
output: none'''
for item in list:
if(
len(item)<MINIMUM_LENGTH):
print item
return None
def delete_item(list):
'''
Function deletes items in 3 different ways.
input: list - grocery list
output: none'''
print_delete_menu()
choice = input()
if(choice is DELETE_ALL_ITEMS):
requested_item = raw_input("Please enter item (Warning! If it'll be deleted, it will not be saved elsewhere): ")
requested_item = requested_item.lower()
if(requested_item in list):
del list[requested_item]
else:
print "item is not in list..."
elif(choice is DELETE_ONE_ITEM):
requested_item = raw_input("Please enter item (Warning! If you have only one of this item, this item will be deleted totally and won't be aved elsewhere): ")
requested_item = requested_item.lower()
if(requested_item in list):
value = list[requested_item]
value -= 1
list[requested_item] = value
if(value is 0):
del list[requested_item]
else:
print "item is not in list..."
else:
print "invalid input..."
return None
def total_sum (list):
sum = 0
for item in list:
sum += list[item]
return sum
def register (users):
username = 0
while(username != "stop"):
username = raw_input("""Enter username, enter "stop" to return to the login menu: """)
if(username in users):
print "username already exist"
else:
users[username] = {}
return username
def login (users):
username = raw_input("Enter your username ")
if(username in users):
return True, username
else:
print "username not exist"
def users_menu (users, list):
access = False
while(not access):
print_login_menu()
choice = input()
if(choice is LOGIN):
access, username = login(users)
elif(choice is REGISTER):
username = register(users)
if(username != "stop"):
get_input(list)
else:
users_menu(users, list)
else:
print "invalid input"
return username
def print_login_menu ():
print "1. login"
print "2. register"
def catalog_list_by_username(users, username, list):
users[username] = list
for item in list:
print item
del list[item]
def main():
grocery_list = {}
users = {}
choice = 0
print users
while(choice is not EXIT):
username = users_menu(users, grocery_list)
choice = 0
print users, "2"
while(choice is not EXIT and choice is not LOGOUT):
print users, "3"
print_menu()
choice = input()
if(choice is WATCH_LIST):
print grocery_list
elif(choice is WATCH_HOW_MANY_ITEMS_IN_LIST):
print len(grocery_list)
elif(choice is CHECK_IF_ITEM_IS_IN_LIST):
item = raw_input("Please enter item ")
print item in grocery_list
elif(choice is SHOW_THE_NUMBER_OF_TIMES_ITEM_IS_IN_THE_LIST):
item = raw_input("Please enter item ")
print_num_of_times_item_is_listed(grocery_list, item)
elif(choice is DELETE_ITEM):
delete_item(grocery_list)
elif(choice is ADD_NEW_ITEM):
add_item_to_the_list(grocery_list)
elif(choice is WATCH_INVALID_ITEMS):
print_invalid_items(grocery_list)
elif(choice is TOTAL_SUM):
total_items_sum = total_sum(grocery_list)
print "the total sum of your list is", total_items_sum
elif(choice is LOGOUT):
print "Goodbye,", username + "..."
catalog_list_by_username(users, username, grocery_list)
elif(choice is not EXIT):
#The choice is neither 10 (exit) nor any of the above choices
print "invalid input..."
print "GoodBye :)"
return None
main()
答案 0 :(得分:0)
您只需在grocery_list
中创建一个main()
,然后将其重复用于所有用户。因此,所有项目都会附加到同一列表中。您可以在main()
中进行以下更改,为每个用户创建单独的列表:
def main():
# change the next line from grocery_list to grocery_lists
# (this dictionary will hold one grocery list for each user)
grocery_lists = {}
users = {}
choice = 0
print users
while(choice is not EXIT):
username = users_menu(users, grocery_list)
# add the next few lines to select the right list for the current user
if username not in grocery_lists:
# make a grocery list for this user
grocery_lists[username] = {}
# retrieve the grocery list for this user
grocery_list = grocery_lists[username]
# code below here is unchanged...