Currently my function is set to print a list of user information in descending order. If there were 3 names; Amy, Tom and Sean, it would print Amy -> Tom and then Sean alphabetically. The code for my main python file is as follows:
from ShowAllRecords import show_record from deleteRecord import delete_student from showRecord import view_records from createRecord import add_record global student_info global database
""" Fields :- ['Student ID', 'First name', 'Last Name', 'age', 'address', 'phone number']
- Create a new Record
- Show a record
- Delete a record
- Display All Records.
- Exit """
student_info = ['Student ID', 'First name', 'last name', 'age', 'address', 'phone number'] database = 'file_records.txt'
def display_menu(): print("") print(" RECORDS MANAGER ") print("") print("1. Create a new record. ") print("2. Show a record. ") print("3. Delete a record. ") print("4. Display All Records. ") print("5. Exit")
while True: display_menu()
choice = input("Enter your choice [1-5]: ")
if choice == '1':
print('You have chosen "Create a new record."')
add_record()
elif choice == '2':
print('You have chosen "Show a record"')
show_record()
elif choice == '3':
print('You have chosen "Delete a record". ')
delete_student()
elif choice == '4':
print('You have chosen "Display ALL records"')
view_records()
else:
print('You have chosen to exit the program. Exiting...')
break
The code for my Show all records function which is currently set to sort in Descending order:
import csv student_info = ['Student ID', 'First name', 'last name', 'age', 'address', 'phone number'] database = 'file_records.txt'
def view_records():
print("--- Student Records ---")
with open(database, "r", encoding="utf-8") as f:
for x in student_info:
print(x, end='\t |')
print("\n-----------------------------------------------------------------")
reader = csv.reader(f)
student_records = sorted(reader, key=lambda r: (r[2], r[1]))
for row in student_records:
print(*row, sep="\t |")
input("Done! Press enter to continue")
print('returning to Main Menu')
What I'm trying to figure out: How could I prompt the user asking if they'd like to sort in either Ascending or Descending order and then do so? Sorry if this doesn't make sense, English is not my first language.
It's currently set to sort alphabetically descending
trying to format this post properly...