简体   繁体   中英

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
{% 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 %}
###############################################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'

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")

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