Shahram Khalid
Shahram Khalid

Reputation: 13

How to fix, TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

Following are 4 files, each separated by a series of "#" pound or hashTag symbols followed by the file name. The code is between grave accent ( ` ) symbols.

Problem is that when this python powered web application is executed, the line number 11 in python file "evenOrOdd.py" is where the error is occuring. When you goto: http://127.0.0.1:5000 on your web browser following error occurs:

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

On clicking this error, it takes me to the line number 11.

On line number 11, python code is:-

elif num%2==0:

On line number 7 is the variable "num" defined as:-

num=int(request.form.get("numberInputFromForm"))

I have tried removing the int() on line 7 of python file "evenOrOdd.py", doesn't work, still gives the same error.

And also I've independently tried to convert "num" variable to int().

###############################################evenOrOdd.py
from flask import Flask, render_template, request

app=Flask("__name__")

@app.route("/", methods=["POST", "GET"])
def indexFunction():
    num=int(request.form.get("numberInputFromForm"))
    dict={'even':False, 'odd':False, 'zero':False, 'number_input':num}
    if num==0:
        dict['zero']=True
    elif num%2==0:
        dict['even']=True
    else:
        dict['odd']=True
    if request.method=="POST":
        return render_template("evenOrOdd.html", dict=dict)
    return render_template("index.html")
###############################################layout.html
<!DOCTYPE html>
<html>
  <head>
    <title>My Web Page</title>

    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">

  </head>

  <body>
      <br>
      <h1>Python powered web app<h1>
      This application tells is a number is even or odd <br>
      {% block f %}  {% endblock %}

    <p>
      {% block p %}  {% endblock %}
    </p>

  </body>
</html>
###############################################index.html
{% extends "layout.html" %}

{% block f %}
  <form  action="{{url_for('indexFunction')}}" method="post">
    <input name='numberInputFromForm' type='number' placeholder="Enter number here...">
    <button> Submit </button>
  </form>
{% endblock %}
###############################################evenOrOdd.html
{% extends "layout.html" %}

{% block p %}
  {% if dict['even'] %}
    <h1> {{dict['number_input']}} is EVEN </h1>
  {% elif dict['odd'] %}
    <h1> {{dict['number_input']}} is ODD </h1>
  {% else %}
    <h1> {{dict['number_input']}} is ZERO </h1>
  {% endif %}
{% endblock %}

{% block f %}
  <form  action="{{url_for('indexFunction')}}" method="post">
    <input name='numberInputFromForm' type='number' placeholder="Enter number here...">
    <button> Submit </button>
  </form>
{% endblock %}

Following error occurs:- TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

Upvotes: 0

Views: 2225

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

To get the form (which I assume is in "index.html") into the browser, there's an initial GET. Unless you pass ?numberInputFromForm=something, numberInputFromForm won't get present, and will present as None.

The fix is to guard the code path that depends on it, such that the path is only taken on a POST. Something like

if request.method == 'POST':
    num=int(request.form.get("numberInputFromForm"))
    ...
    return render_template("evenOrOdd.html", ...)
else:
    return render_template("index.html")

Upvotes: 1

Related Questions