Nisa Yathu
Nisa Yathu

Reputation: 39

Grade not displayed successfully using python

I am a beginner in python programming. I scripted a system to compute student marks.

Everything works as intended, but I get fail displayed once. Also, if average is more than 50 I also get a fail message. I can't understand why. Here is my code

 from tkinter import *
    
    def Ok():
        result = int(e1.get()) + int(e2.get()) + int(e3.get())
    
        totText.set(result)
    
        average = result/3
        avgText.set(average)
    
        if (average > 50) :
                grade = "pass"
        else :
            grade = "fail"
    
            gradeText.set(grade)
    
    root = Tk()
    root.title("Calculator")
    root.geometry("300x400")
    
    global e1
    global e2
    global e3
    global totText
    global avgText
    global gradeText
    
    
    
    totText = StringVar()
    avgText = StringVar()
    gradeText = StringVar()
    
    
    Label(root, text="Marks1").place(x=10, y=10)
    Label(root, text="Marks2").place(x=10, y=40)
    Label(root, text="Marks3").place(x=10, y=80)
    Label(root, text="Total:").place(x=10, y=110)
    Label(root, text="Avg:").place(x=10, y=140)
    Label(root, text="Grade:").place(x=10, y=180)
    e1 = Entry(root)
    e1.place(x=100, y=10)
    
    e2 = Entry(root)
    e2.place(x=100, y=40)
    
    e3 = Entry(root)
    e3.place(x=100, y=80)

    result = Label(root, text="", textvariable=totText).place(x=100, y=110)
    avg = Label(root, text="", textvariable=avgText).place(x=100, y=140)
    grade = Label(root, text="", textvariable=gradeText).place(x=100, y=180)

    Button(root, text="Cal", command=Ok ,height = 1, width = 3).place(x=10, y=220)

    marks1 = Entry(root)
    marks2 = Entry(root)
    marks3 = Entry(root)
    
    
    
    root.mainloop()

Upvotes: 0

Views: 79

Answers (2)

J0ki
J0ki

Reputation: 302

Format your code so:

    if (average > 50):
        grade = "pass"
    else:
        grade = "fail"

    gradeText.set(grade)

Instead of:

    if (average > 50):
        grade = "pass"
    else:
        grade = "fail"

        gradeText.set(grade)

As u can see now u set de gradeText outside the else condition.

Edit: Format code in python is so important (as in every other language) be careful.

Upvotes: 1

Daemon Painter
Daemon Painter

Reputation: 3490

Python enforces a strong indentation system. Check this out:

    if (average > 50) :
            grade = "pass"
    else :
        grade = "fail"

        gradeText.set(grade)

Here grade = "pass" is too much indented. This causes the else statement to be skipped as well, probably with some warning. The next instruction to be executed is then grade = "fail" and gradeText.set(grade). Please check your code with proper indentation

    if (average > 50) :
        grade = "pass"
    else :
        grade = "fail"

    gradeText.set(grade)

Upvotes: 0

Related Questions