2

I am making a program which makes a mark list with a letter grade in python. But the problem is the if-else statements to decide letter grade is only executing else statement when given inputs. But the if statement for 'A+' grade is working if maximum marks are given. I am a newbie and I can't understand where the bug is, please help. Below is my python code, here datails.html collects information from the user and mark list is displayed in the results.html page.

from flask import Flask, render_template, request
app=Flask(__name__)

@app.route("/")
def index():
return render_template('details.html')
@app.route("/send",methods=['POST','GET'])
def send():

if(request.method=='POST'):
    getname=request.form['name']
    getregno=request.form['regno']
    getcollege=request.form['college']
    getsem=request.form['sem']
    getsub1=request.form['sub1']
    getsub1m=request.form['sub1m']
    getsub2=request.form['sub2']
    getsub2m=request.form['sub2m']
    getsub3=request.form['sub3']
    getsub3m=request.form['sub3m']
    getsub4=request.form['sub4']
    getsub4m=request.form['sub4m']

    malayalam_mark = int(getsub1)
    malayalam_maxmark = int(getsub1m)
    english_mark = int(getsub2)
    english_maxmark = int(getsub2m)
    maths_mark = int(getsub3)
    maths_maxmark = int(getsub3m)
    computer_mark = int(getsub4)
    computer_maxmark = int(getsub4m)

    percent_malayalam = malayalam_mark/malayalam_maxmark*100
    percent_english = english_mark/english_maxmark*100
    percent_maths = maths_mark/maths_maxmark*100
    percent_computer = computer_mark/computer_maxmark*100

    slist=  [percent_malayalam,percent_english,percent_maths,percent_computer]

    result_list=[]

    for i in slist:

            if i>=50 and i<58:

                    grade='D+'
                    result='pass'

            elif i>=58 and i<65:

                    grade='C'
                    result='Pass'

            elif i>=65 and i<72:

                    grade='C+'
                    result='Pass'

            elif i>=72 and i<79:

                    grade='B'
                    result='Pass'

            elif i>=79 and i<86:

                    grade='B+'
                    result='Pass'

            elif i>=86 and i<93:

                    grade='A'
                    result='Pass'

            elif i>=93 and i<=100:

                    grade='A+'
                    result='Pass'

            else:
                    grade='D'
                    result='Fail'



            result_list.append(grade)
            result_list.append(result)


    return render_template('/results.html',list=result_list,a=getname,b=getregno,c=getsem,d=getcollege,e=getsub1,e1=getsub1m,f=getsub2,f1=getsub2m,g=getsub3,g1=getsub3m,h=getsub4,h1=getsub4m)

if(__name__=='__main__'):
    app.run(debug=True)

I am including a result page, here I included the final list in the bottom for reference, you can find that every input gives grade 'D' and result 'Fail' except maximum marks.

Vivi
  • 41
  • 1
  • 7
  • 3
    Whatever is being evaluated is not evaluating to true on any of them. Check your input data, verify what you're expecting then debug and follow the data through to verify that the data you expect exists. If the data is exactly what you expect then the if logic is broken, if the data is not what you're expecting then manipulate the data or alter the if logic to match. Likely something in the initial data manipulation is not outputting what you're expecting. – Lady_A Nov 16 '18 at 17:34
  • Any chance you can print some of the interim variables, like getsub1 and malayalam_maxmark? The math might be wrong because those variables aren't turning into what you'd expect. Also, anything from details.html (anonymized/scrubbed, of course) would be helpful to see as well! – user2752159 Nov 16 '18 at 17:36
  • 2
    Confirm that `slist` looks like what you think it looks like – tom Nov 16 '18 at 17:36
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. In particular, if your problem is with the operation of the grade assignment, then the code above that point should be only assigning a short sequence to `slist`. – Prune Nov 16 '18 at 17:41
  • The if/else statements have been proven to be reliable millions if not billions of times over. The only valid conclusion you can draw is that your data isn't what you think it is – Bryan Oakley Nov 16 '18 at 17:42
  • Sorry for this unreadable code, this is the first time I am posting on this site. What I am planning to do is, I append the values for grade and result to result_list and take that values by indexing to display it on the results.html page. – Vivi Nov 16 '18 at 17:47

1 Answers1

3

I guess this is being run in Python 2 which doesn't automatically promote integer division. If you rearrange your calculations like:

    percent_malayalam = malayalam_mark * 100 / malayalam_maxmark

you might get the right answer… Previously all non-100% results were being (implicitly) rounded to 0%. 2 / 3 in Python 2 will be 0, not ~0.6. The defaults have been changed in Python 3 so it tends to do the right thing more often.

You could then read about integer division in Python 2 from various places, here's one at this site: What is the difference between '/' and '//' when used for division?

Sam Mason
  • 15,216
  • 1
  • 41
  • 60