in if else condition, this program doesn’t run code that is in else:

  flask, if-statement, python

I will paste the code below and then explain

from flask import Flask,render_template,url_for,request,redirect
import pandas as pd
import glob
import os
import pickle

# load the model from disk
# here we load random forest model as it gave good results when compared
loaded_model=pickle.load(open('pickle-files/RandomForestRegressor.pkl', 'rb'))
app = Flask(__name__)

@app.route('/',methods = ['POST', 'GET'])
def home():
   if request.method == 'POST':
        year = request.form['value']
        if len(year) == 0:
            return render_template('home.html')
        elif year != 2013 or year != 2014 or year != 2015 or year != 2016 or year != 2017 or year != 2018:
            return render_template('home.html', num = 'You did not enter the above year correctly')
        else:
            df=pd.read_csv('Data/Real-Data/real_{}.csv'.format(year))
            my_prediction=loaded_model.predict(df.iloc[:,:-1].values)
            my_prediction=my_prediction.tolist()
            return render_template('home.html', prediction = my_prediction)
   else:
       return render_template('home.html')




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

When I run the app on the host everything works fine except the code in the else block is not touched by the program.

Let’s assume the year = 2013, it should get into the else block do the prediction work, and return its results on the web page.

I will paste the screenshot of the web application here
Note: please ignore the barbrothers.com in the image

I am expecting the prediction to show like this below screenshot

Rather, I get like this below screenshot enter image description here

I tried many different ways to work this right but it is not predicting the values that I want which is 2013 2014 2015 2016 2017 2018 2013-2018.

I hope my issue is understood, please let me know.

Source: Python Questions

LEAVE A COMMENT