HTMLify

qcfc.py
Views: 166 | Author: abh
#quiz ans cheker for college
#07/10/2023

def ans():
    return open("ans.txt").read().split("\n")[:-1]

def std(no):
    try:
        std_data = open(str(no)+".txt").read().split("\n")
    except:
        return None
    s = {
        "sno": no,
        "name": std_data[0],
        "sem": std_data[1],
        "ans": std_data[2:]
    }
    return s
    
def result():
    stds = []
    answers = ans()
    no = 1
    while s:= std(no):
        marks = 0
        corrects = []
        for n in range(len(ans())):
            if answers[n] == s["ans"][n]:
                marks += 1
                corrects.append(True)
            else:
                corrects.append(False)
        s["marks"] = marks
        s["correct"] = corrects
        stds.append(s)
        no += 1    
    stds = sorted(stds, key = lambda s:s["marks"])[::-1]
    return stds    
    

o = input()

if o == "feed":
    while True:
        sno = 0
        while True:
            try:
                sno += 1
                open(str(sno)+".txt")
            except:
                break
        
        # feeding
        name = input("Enter name for s no " + str(sno) + " ")
        sem = input("Enter semester ")
        
        data = name + "\n" + sem
        
        print("Enter marks in order:\n Q A")
        for q in range(1, len(ans())+1):
            data += "\n" + input(str(q).zfill(2) + " ")
        
        with open(str(sno)+".txt", 'w+') as s:
            s.write(data)
        next = input("data succesfully saved for " + str(sno) + " want to feed more?")
        if not next: break

if o == "result":
    for s in result():
        print(s)

if o == "html":
    style = "<style>.correct {\ncolor:green;}\n.wrong {\ncolor:red;}</style>\n"
    html = "<table>\n<tr><td>S no.</td><td>Name</td><td>Semester</td>"
    for i in range(1, len(ans())+1):
        html += "<td>" + str(i) + "</td>"
    html += "<td>Marks</td></tr>\n"
    n = 0
    for s in result():
        n += 1
        html += "<td>" + str(n) + "</td><td>" + s["name"] + "</td><td>" + s["sem"] + "</td>"
        for correct, ans in zip(s["correct"], s["ans"]):
            html += "<td class=\"" + ("correct" if correct else "wrong") + "\">" + ans + "</td>"
        html += "<td>" + str(s["marks"]) + "</td></tr>"
    html += "</table>"        
    print(style, html)

Comments