[英]How to fix “TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'”
[英]How to fix, TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
以下是4个文件,每个文件由一系列“#”井号或hashTag符号分隔,后跟文件名。 该代码位于重音符号(`)之间。
问题是执行此python供电的Web应用程序时,发生错误的位置是python文件“ evenOrOdd.py”中的行号11。 当您在Web浏览器上转到: http : //127.0.0.1 : 5000时 ,发生以下错误:
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
点击此错误后,我进入第11行。
在第11行,python代码为:-
elif num%2==0:
在第7行上,变量“ num”定义为:
num=int(request.form.get("numberInputFromForm"))
我试过删除python文件“ evenOrOdd.py”的第7行上的int(),不起作用,仍然给出相同的错误。
而且我也独立尝试将“ num”变量转换为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 %}
发生以下错误:-TypeError:%不支持的操作数类型:'NoneType'和'int'
为了将表单(我认为在“ index.html”中)放入浏览器,需要进行初始GET。 除非您通过?numberInputFromForm=something
, numberInputFromForm
将不会显示,并且将显示为None
。
解决方法是保护依赖它的代码路径,以使该路径仅在POST上采用。 就像是
if request.method == 'POST':
num=int(request.form.get("numberInputFromForm"))
...
return render_template("evenOrOdd.html", ...)
else:
return render_template("index.html")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.