如何修复,TypeError:% 不支持的操作数类型:'NoneType' 和 'int'

沙赫拉姆哈立德

以下是 4 个文件,每个文件由一系列“#”井号或 hashTag 符号分隔,后跟文件名。代码位于重音符 (`) 符号之间。

问题是当这个由 py​​thon 驱动的 web 应用程序被执行时,python 文件“evenOrOdd.py”中的第 11 行是发生错误的地方。当您转到: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
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 %}

发生以下错误:- TypeError:% 不支持的操作数类型:'NoneType' 和 'int'

戴夫·W·史密斯

要将表单(我假设在“index.html”中)放入浏览器,有一个初始 GET。除非你通过?numberInputFromForm=somethingnumberInputFromForm否则不会出现,并且会以None.

修复是保护依赖于它的代码路径,这样路径只在 POST 时被采用。就像是

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Keras报告TypeError:+不支持的操作数类型:'NoneType'和'int'

H2O异常TypeError:+不支持的操作数类型:'NoneType'和'unicode'

TypeError:+时不支持的操作数类型:求和列表时为“ int”和“ NoneType”

*:Python 3中不支持*:'NoneType'和'int'的操作数类型

TypeError:打印时%不支持的操作数类型:'NoneType'和'int'

处理Bagging方法时,如何解决TypeError:+不支持的操作数类型:“ int”和“ NoneType”

如何修复TypeError:+不支持的操作数类型:“ int”和“ list”

TypeError:&:不支持的操作数类型:“ NoneType”和“ str”

训练GRU模型TypeError时出现此错误:*不支持的操作数类型:'int'和'NoneType'

Python:TypeError:+不支持的操作数类型:“ NoneType”和“ int”

TensorFlow 1.x:TypeError:/:不支持的操作数类型:'NoneType'和'int'

TypeError:+不支持的操作数类型:“ NoneType”和“ list”

TypeError:+ =不支持的操作数类型:“ datetime.timedelta”和“ NoneType”

TypeError:%不支持的操作数类型:“ NoneType”和“ int”

Python错误:+不支持的操作数类型:“ int”和“ NoneType”

+不支持的操作数类型:'NoneType'和'str'和'int'

TypeError:+不支持的操作数类型:“ NoneType”和“ int”

TypeError:+ =:Python 3中不支持的操作数类型为“ float”和“ NoneType”

TypeError: 不支持的操作数类型 +: 'NoneType' 和 'int' 在 while 循环中

类型错误:* 不支持的操作数类型:'int' 和 'NoneType'

如何修复 TypeError: 不支持的操作数类型 +: 'int' 和 'str'

如何修复“TypeError 不支持/的操作数类型:‘int’和‘function’”?

递归阶乘报告错误`TypeError:*不支持的操作数类型:'int'和'NoneType'`

如何处理 TypeError: 不支持的操作数类型 -: 'list' 和 'int'?

出现错误“TypeError:% 不支持的操作数类型:‘NoneType’和‘str’”

TypeError: 不支持的操作数类型 *: 'float' 和 'NoneType' 在构建 pyomo 模型时

TypeError: 不支持的操作数类型 +: 'NoneType' 和 'str'"?

试图解决“TypeError:+ 不支持的操作数类型:“NoneType”和“int”

蟒蛇 | TypeError: 不支持的操作数类型 /: 'NoneType' 和 'int' |使用变量来划分