如果语句仅执行else块

时间:2018-11-16 17:30:27

标签: python flask

我正在制作一个程序,该程序在python中用字母等级制作标记列表。 但是问题是,决定字母等级的if-else语句仅在给定输入时才执行else语句。但是,如果给出最大分数,则“ A +”等级的if语句将起作用。我是新手,无法理解错误所在,请提供帮助。以下是我的python代码,此处datails.html从用户那里收集信息,标记列表显示在results.html页面中。

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.

1 个答案:

答案 0 :(得分:3)

我猜这是在Python 2中运行的,它不会自动促进整数除法。如果您像这样重新排列计算:

    percent_malayalam = malayalam_mark * 100 / malayalam_maxmark

您可能会得到正确的答案...以前,所有非100%的结果都(隐式地)四舍五入为0%。 Python 2中的2 / 3将为0,而不是〜0.6。默认值已在Python 3中更改,因此它倾向于更经常地执行正确的操作。

然后您可以从不同的地方阅读有关Python 2的整数除法的信息,这是此站点的内容:What is the difference between '/' and '//' when used for division?