绝对可以短一点,但是我是菜鸟

时间:2019-06-23 07:19:52

标签: python-3.x

我不想一遍又一遍地恢复打印。尽管不确定c1到c4随每个if语句的位置而变化,但是不确定如何为此过程创建函数。我的问题是,具体来说,我打算如何将此代码隐含起来并使其变得更小,因为我计划在此列表中添加更多的类(最多15-20个)。

if c1 == M1:
    print("\n")
    print("Monday 1", c1)
    print("Monday 2", c2)
    print("Tuesday 1", c3)
    print("Tuesday 2", c4)
    counting(c1, c2, c3, c4)
elif c1 == M2:
    print("\n")
    print("Monday 1", c4)
    print("Monday 2", c1)
    print("Tuesday 1", c2)
    print("Tuesday 2", c3)
    counting(c1, c2, c3, c4)
elif c1 == T1:
    print("\n")
    print("Monday 1", c3)
    print("Monday 2", c4)
    print("Tuesday 1", c1)
    print("Tuesday 2", c2)
    counting(c1, c2, c3, c4)
elif c1 == T2:
    print("\n")
    print("Monday 1", c2)
    print("Monday 2", c3)
    print("Tuesday 1", c4)
    print("Tuesday 2", c1)
    counting(c1, c2, c3, c4)

这是要求的完整代码,因为我无法蛮力回答。

#All the students and their avalable days/times
students = ("Alec", "M1", "T1"), ("Ben", "M1", "M2", "T1"), ("Carl", "M1", "T1"), ("David", "M1", "M2", "T1"), ("Elvis", "M1", "M2", "T1", "T2"),\
           ("Fiona", "M1", "M2", "T1", "T2"), ("Gage", "M1", "M2", "T2"), ("Henry", "T1", "T2"), ("Isabel", "T2"), ("Jack", "T2")


#All classes
M1 = []
M2 = []
T1 = []
T2 = []

#Lessons the need for multiple lines of repeated code
def add_student(class_name, classes):
    if class_name in student:
        classes.append(student[0])

#Checks if class has minimum of 4 students
def counting(c1, c2, c3, c4):
        count = 0
        if len(c1) >= 4:
            count += len(c1)
        if len(c2) >= 4:
            count += len(c2)
        if len(c3) >= 4:
            count += len(c3)
        if len(c4) >= 4:
            count += len(c4)
        print("{:,}".format(count * 50000), "VND")


def first_sort(a, b, c, d):
    c1 = a.copy()
    c2 = b.copy()
    c3 = c.copy()
    c4 = d.copy()
    all_classes = (c1, c2, c3, c4)
    remove = []

    for student in students:
        for i in all_classes:
            if student[0] in remove:
                try:
                    i.remove(student[0])
                except:
                    pass
            elif student[0] in i:
                remove.append(student[0])

    if c1 == M1:
        print("\n")
        print("Monday 1", c1)
        print("Monday 2", c2)
        print("Tuesday 1", c3)
        print("Tuesday 2", c4)
        counting(c1, c2, c3, c4)
    elif c1 == M2:
        print("\n")
        print("Monday 1", c4)
        print("Monday 2", c1)
        print("Tuesday 1", c2)
        print("Tuesday 2", c3)
        counting(c1, c2, c3, c4)
    elif c1 == T1:
        print("\n")
        print("Monday 1", c3)
        print("Monday 2", c4)
        print("Tuesday 1", c1)
        print("Tuesday 2", c2)
        counting(c1, c2, c3, c4)
    elif c1 == T2:
        print("\n")
        print("Monday 1", c2)
        print("Monday 2", c3)
        print("Tuesday 1", c4)
        print("Tuesday 2", c1)
        counting(c1, c2, c3, c4)


#Checks if all the students are in a class then adds those who are into the class
for student in students:
    add_student("M1", M1)
    add_student("M2", M2)
    add_student("T1", T1)
    add_student("T2", T2)

#Prints all the classes with all students who could attend said class
print("Monday 1", M1)
print("Monday 2", M2)
print("Tuesday 1", T1)
print("Tuesday 2", T2)

#Sorts through all classes and displays with start/delete
first_sort(M1, M2, T1, T2)
first_sort(M2, T1, T2, M1)
first_sort(T1, T2, M1, M2)
first_sort(T2, M1, M2, T1)

1 个答案:

答案 0 :(得分:0)

好像您想“旋转” c1-c4列表一样,尊重星期一/星期二的字符串。

这应该可以解决问题:

    days = ["Monday 1", "Monday 2", "Tuesday 1", "Tuesday 2"]
    cs_original = [c1, c2, c3, c4]
    m_and_ts = [M1, M2, T1, T2]

    c1_index_in_conditions = m_and_ts.index(c1)
    cs_rotated = cs_original[-c1_index_in_conditions:] + cs_original[:-c1_index_in_conditions]

    days_with_cs = zip(days, cs_rotated)

    print("\n")
    for day_w_c in days_with_cs:
        print(*day_w_c)

    counting(*cs_original)

包含所有代码的完整解决方案:

#All the students and their avalable days/times
students = ("Alec", "M1", "T1"), ("Ben", "M1", "M2", "T1"), ("Carl", "M1", "T1"), ("David", "M1", "M2", "T1"), ("Elvis", "M1", "M2", "T1", "T2"),\
           ("Fiona", "M1", "M2", "T1", "T2"), ("Gage", "M1", "M2", "T2"), ("Henry", "T1", "T2"), ("Isabel", "T2"), ("Jack", "T2")


#All classes
M1 = []
M2 = []
T1 = []
T2 = []

#Lessons the need for multiple lines of repeated code
def add_student(class_name, classes):
    if class_name in student:
        classes.append(student[0])

#Checks if class has minimum of 4 students
def counting(c1, c2, c3, c4):
        count = 0
        if len(c1) >= 4:
            count += len(c1)
        if len(c2) >= 4:
            count += len(c2)
        if len(c3) >= 4:
            count += len(c3)
        if len(c4) >= 4:
            count += len(c4)
        print("{:,}".format(count * 50000), "VND")


def first_sort(a, b, c, d):
    c1 = a.copy()
    c2 = b.copy()
    c3 = c.copy()
    c4 = d.copy()
    all_classes = (c1, c2, c3, c4)
    remove = []

    for student in students:
        for i in all_classes:
            if student[0] in remove:
                try:
                    i.remove(student[0])
                except:
                    pass
            elif student[0] in i:
                remove.append(student[0])

    days = ["Monday 1", "Monday 2", "Tuesday 1", "Tuesday 2"]
    cs_original = [c1, c2, c3, c4]
    m_and_ts = [M1, M2, T1, T2]

    c1_index_in_conditions = m_and_ts.index(c1)
    cs_rotated = cs_original[-c1_index_in_conditions:] + cs_original[:-c1_index_in_conditions]

    days_with_cs = zip(days, cs_rotated)

    print("\n")
    for day_w_c in days_with_cs:
        print(*day_w_c)

    counting(*cs_original)


#Checks if all the students are in a class then adds those who are into the class
for student in students:
    add_student("M1", M1)
    add_student("M2", M2)
    add_student("T1", T1)
    add_student("T2", T2)

#Prints all the classes with all students who could attend said class
print("Monday 1", M1)
print("Monday 2", M2)
print("Tuesday 1", T1)
print("Tuesday 2", T2)

#Sorts through all classes and displays with start/delete
first_sort(M1, M2, T1, T2)
first_sort(M2, T1, T2, M1)
first_sort(T1, T2, M1, M2)
first_sort(T2, M1, M2, T1)

基本上,它会找到位置c1应该位于的位置,然后将每个c*与相应的字符串配对...