I am new to programming and Stackoverflow. I am currently working on file handling and classes. The program is working, but my question is if this part of the code is acceptable? list_of_objects is a list with "[]" that contains class objects.
def menu_choice_function(list_of_objects):
while True:
menu()
choice = input("Choice: ")
if choice == "1":
print("Create a packing list")
list_of_objects = create_packing_list(list_of_objects)
elif choice == "2":
print("Show the packing list content")
show_packing_list_content(list_of_objects)
elif choice == "3":
print("Append or delete items in the packing list")
list_of_objects = app_del_items(list_of_objects)
elif choice == "4":
print("Show all packing lists")
show_all_packing_list(list_of_objects)
elif choice == "5":
print("Edit the packing list name or date")
list_of_objects = edit_name_date(list_of_objects)
elif choice == "6":
print("Remove a packing list")
list_of_objects = remove_packing_list(list_of_objects)
elif choice == "7":
print("Add reminder or remove reminder for the packing list")
list_of_objects = add_remove_reminder(list_of_objects)
elif choice == "8":
return list_of_objects
else:
print("Please enter a choice.")
What I mean by if it is acceptable is that you can see that some functions return a modified list but they all have the same name as parameters variables. I did this because I was thinking that you can modify a list variable. But I am wondering if this method will bring some consequences. It is working so far because so far no errors or weird behavior in the program. Sorry for my bad English. I hope you understand what I am trying to ask.
CodePudding user response:
Let's say, for the sake of this example, that your functions are named func1-7 and that each one takes a list which it uses for reference or maybe modifies it. Remember, that lists are passed be reference so you can change them in your function(s).
Furthermore, you can make the entire menu system parameter driven. This makes it much easier to add or remove functionality.
Consider this:
def func1(loo):
print('Executing func1')
def func2(loo):
print('Executing func2')
def func3(loo):
print('Executing func3')
def func4(loo):
print('Executing func4')
def func5(loo):
print('Executing func5')
def func6(loo):
print('Executing func6')
def func7(loo):
print('Executing func7')
PARAMS = {1: ['Create a packing list', func1],
2: ['Show the packing list content', func2],
3: ['Append or delete items in the packing list', func3],
4: ['Show all packing lists', func4],
5: ['Edit the packing list name or date', func5],
6: ['Remove a packing list', func6],
7: ['Add reminder or remove reminder for the packing list', func7]
}
def menu(list_of_objects):
while True:
for k, v in PARAMS.items():
print(f'{k}: {v[0]}')
try:
if (i := int(input('Please select an option from the menu or zero to end: '))) == 0:
break
if (a := PARAMS.get(i, None)):
a[1](list_of_objects)
except ValueError:
pass
menu([])
CodePudding user response:
You can use the keyword global to access and modify your list inside a function and so you won't have to keep passing it to every function you call. Check this tutorial on how to use it.
As for if it's acceptable or not I'd say that the parameters and variables having the same name lowers the readability of the code so I'd change the naming.
CodePudding user response:
It is not wrong as long as it works, but for sake of readablity and simplicity and from my prespective on your code:
If you only want to initalize or populate list then, you dont need to pass it to function unless you need the data from it.
It is good in the if chains above dont print messeages with magic vaules, instead save your message in a variable and print it in the last.
These are the only things that I can tell you if something else just comment it.