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

YEEF

有谁知道为什么我不断收到此错误?我真的很新,很感谢别人的帮助。这是我的代码:

import turtle as t
import math as m
import random as r

raindrops = int(input("Enter the number of raindrops: "))

def drawSquare():
    t.up()
    t.goto(-300,-300)
    t.down()
    t.fd(600)
    t.lt(90)
    t.fd(600)
    t.lt(90)
    t.fd(600)
    t.lt(90)
    t.fd(600)
    t.lt(90)

def location():
    x = (r.randint(-300, 300))
    y = (r.randint(-300, 300))
    t.up()
    t.goto(x, y)
    return x, y

def drawRaindrops(x, y):
    t.fillcolor(r.random(), r.random(), r.random())
    circles = (r.randint(3, 8))
    radius = (r.randint(1, 20))
    newradius = radius
    area = 0
    t.up()
    t.rt(90)
    t.fd(newradius)
    t.lt(90)
    t.down()
    t.begin_fill()
    t.circle(newradius)
    t.end_fill()
    t.up()
    t.lt(90)
    t.fd(newradius)
    t.rt(90)
    while circles > 0:
        if x + newradius < 300 and x - newradius > -300 and y + newradius < 300 and y - newradius > -300:
            t.up()
            t.rt(90)
            t.fd(newradius)
            t.lt(90)
            t.down()
            t.circle(newradius)
            t.up()
            t.lt(90)
            t.fd(newradius)
            t.rt(90)
            newradius += radius
            circles -= 1
            area += m.pi * radius * radius
        else:
            circles -= 1
    return area

def promptRaindrops(raindrops):
    if raindrops < 1 or raindrops > 100:
        print ("Raindrops must be between 1 and 100 inclusive.")
    if raindrops >= 1 and raindrops <= 100:
        x, y = location()
        area = drawRaindrops(x, y)
        area += promptRaindrops(raindrops - 1)
        return x, y, area

def main():
    t.speed(0)
    drawSquare()
    x, y, area = promptRaindrops(raindrops)
    print('The area is:', area, 'square units.')

main()
t.done()

我假设“ + =”出了点问题,但我不知道该怎么办。我相当确定该区域已正确返回。请帮助。:)

尼尔·A。

我注意到两件事:

1.promptRaindrops返回一个元组

我确定您并没有打算这样做,但是当您说时area += promptRaindrops(raindrops - 1),您要向其中添加一个元组area,它是一个整数要解决此问题,您应该说area += promptRaindrops(raindrops - 1)[2]要返回该区域。但是,您的错误是由

2.您的基本情况不返回值

在中promptRaindrops,您每当返回函数的递归调用1 <= raindrops <= 100但是,当它超出该范围时,它不返回任何内容,仅显示一条消息。您的函数将始终在该范围之外,因为如果您继续减小传递给的值promptRaindrops,则它最终将低于1。当它返回时,您将返回None(因为未返回任何内容)。在这一点None上进行的每个递归调用中都会冒出气泡,您将不可避免地将添加Nonearea添加一个返回元组的return语句,您的错误应消失。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

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

python函数处理给出错误TypeError:不支持的操作数类型-:'NoneType'和'NoneType'

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

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

Python3:socket:TypeError:%不支持的操作数类型:“字节”和“字节”

Python 3 TypeError:**或pow()不支持的操作数类型:“ str”和“ int”

Python TypeError:^:'float'和'int'不支持的操作数类型

TypeError:python 3.x Anaconda中-:'str'和'str'的不支持的操作数类型

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

loguru 中的文字和列表的 Python 错误:+ 不支持的操作数类型:'NoneType' 和 'list'

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

* 不支持的操作数类型:“NoneType”和“NoneType”

TypeError: 不支持的操作数类型 +: 'Timedelta' 和 'datetime.time - python