简体   繁体   中英

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' error in python3

I am building a heart disease prediction website. The following code is giving a typeerror. In the html file it is of type text and it gets into type int in cp.

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' I have also used pickel to open the file.

sc = pickle.load(open('sc.pkl', 'rb'))
model = pickle.load(open('model.pkl', 'rb'))

def predict():
    lst = []
    cp = int(request.form.get('chest pain type (4 values)'))
    if cp == 0:
        lst += [1 , 0 ,0 ,0]
    elif cp == 1:
        lst += [0 ,1 ,0 ,0]
    elif cp == 2:
        lst += [0 ,0 ,1 ,0]
    elif cp >= 3:
        lst += [0 ,0 ,0 ,1]
    trestbps = int(request.form["resting blood pressure" ])
    lst += [trestbps]
    chol = int(request.form["serum cholestoral in mg/dl"])
    lst += [chol]
    fbs = int(request.form["fasting blood sugar > 120 mg/dl"])
    if fbs == 0:
        lst += [1 , 0]
    else:
        lst += [0 , 1]
    restecg = int(request.form["resting electrocardiographic results (values 0,1,2)"])
    if restecg == 0:
        lst += [1 ,0 ,0]
    elif restecg == 1:
        lst += [0 ,1 ,0]
    else:
        lst += [0 , 0,1]
    thalach = int(request.form["maximum heart rate achieved"])
    lst += [thalach]
    exang = int(request.form["exercise induced angina"])
    if exang == 0:
        lst += [1 , 0]
    else:
        lst += [0 ,1 ]
    final_features = np.array([lst])
    pred = model.predict( sc.transform(final_features))
    return render_template('checkup.html', prediction = pred)

The type error is at cp = int(request.form.get('chest pain type (4 values)')). This is my html file

<form action="/predict"method="POST">
    <input type="text" name="chest pain type (4 values)" placeholder="chest pain type (4 values)" required="required" />
    <input type="text" name="resting blood pressure" placeholder="resting blood pressure" required="required" /> 
  <input type="text" name="serum cholestoral in mg/dl" placeholder="serum cholestoral in mg/dl" required="required" />
  <input type="text" name="fasting blood sugar > 120 mg/dl" placeholder="fasting blood sugar > 120 mg/dl" required="required" />  
  <input type="text" name="resting electrocardiographic results (values 0,1,2)" placeholder="resting electrocardiographic results (values 0,1,2)" required="required" />
    <input type="text" name="maximum heart rate achieved" placeholder="maximum heart rate achieved" required="required" />
    <input type="text" name="exercise induced angina" placeholder="exercise induced angina" required="required" />
    <button type="submit" class="registerbtn">Predict</button>
</form>

This id because your code is trying to convert NoneType into an integer, which Python don't permits.

In your code if request.form.get('chest pain type (4 values)')) the key is not present, it'll return None which is causing the error.

If you have some default value you can use something like request.form.get('chest pain type (4 values)', '0')) So that if the key is not present the code returns the default value rather than None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM