0
0 комментариев

Написать программу, которая через меню предоставляла возможность создавать файл для записи, добавлять записи, редактировать, удалять и т.д. В программе дополнительно предусмотреть возможность для манипуляции списка сущностей соответствующей предметной области (к примеру, отсортировать список по заданному критерию, осуществить необходимы поиск и т.д.).

Где ошибка в функции sorting()?

function.py

import pickle
 
def file_open():
    inf = open("pickle.pkl", "rb")
    air_list = pickle.load(inf)
    inf.close()
    return air_list
 
def file_close(air_list):
    fout = len(air_list)
    out = open("pickle.pkl", "wb")
    pickle.dump(air_list, out, fout)
    out.close
 
def addAirplane(air_list):
    list1 = [[],[],[],[],[],[],[]]
    list1[0] = input("\n Enter ID: ")
    list1[1] = input(" Enter destination: ")
    list1[2] = input(" Enter flight number: ")
    list1[3] = input(" Enter aircraft type: ")
    list1[4] = input(" Enter departure time: ")
    list1[5] = input(" Enter day of week: ")
    air_list.append(list1)
 
def delete(air_list):
    count = 0
    id = int(input(" Enter ID: "))
    for i in range(len(air_list)):
        if(id ==  int(air_list[i][0])):
            count = 1
            air_list.remove(air_list[i])
            break
    if (not count):
        print("There are not record with this ID.")
 
def show(air_list):
    print("\t Information \n")
    for i in range(len(air_list)):
        print(" ID: ", air_list[i][0])
        print(" Destination: ",air_list[i][1])
        print(" Flight number: ",air_list[i][2])
        print(" Aircraft type: ",air_list[i][3])
        print(" Departure time: ",air_list[i][4])
        print(" Day fo week: ",air_list[i][5])
        print("\n")
    return
 
def search(air_list):
    count = 0
    id = int(input(" Enter ID: "))
    for i in range(len(air_list)):
        if(id ==  int(air_list[i][0])):
            count += 1
            print("\n ID: ", air_list[i][0])
            print(" Destination: ",air_list[i][1])
            print(" Flight number: ",air_list[i][2])
            print(" Aircraft type: ",air_list[i][3])
            print(" Departure time: ",air_list[i][4])
            print(" Day fo week: ",air_list[i][5])
            print("\n")
    if (not count):
        print("There are not record with this ID.")
 
def sorting(air_list, x):
    air_list1 = []
    m = 0
    for i in air_list:
        sotr_el = []
        sort_el.append(m)
        sort_el.append(int(i[x]))
        air_list1.append(sort_el)
        m += 1
    air_list1.sort(key=lambda air_list1:air_list1[1])
    for i in range(len(air_list1)):
        print(" ID: ",air_list1[i][0])
        print(" Destination: ",air_list1[i][0][0])
        print(" Flight number: ",air_list1[i][0][1])
        print(" Aircraft type: ",air_list[i][0][2])
        print(" Departure time: ",air_list1[i][0][3])
        print(" Day fo week: ",air_list1[i][0][4])
        print("\n")
    return
 
def menu():
    n = int(input("""\n\tMenu: \n
    1. Show all the record
    2. Add record
    3. Delete record
    4. Search records
    5. Sort of records
    6. Exit
 
Enter your choice: """))
 
return n

main.py

import pickle
from function import *
 
air_list = []
 
while True:
    air_list = file_open()
    n = menu()
 
    if (n == 1):
        show(air_list)
    if (n == 2):
        addAirplane(air_list)
    if (n == 3):
        delete(air_list)
    if (n == 4):
        search(air_list)
    if (n == 5):
        x = int(input(("\n 0 - Sorting by ID,\n 1 - Sorting by fligh number)\n")))
        sorting(air_list, x)
    if (n == 6):
        break
    file_close(air_list)
 
input(" Press enter for exit...")


Добавить комментарий