TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType' in Python 3

theYEEF

Does anyone know why I keep getting this error? I'm really new and I'd appreciate someone's help. This is my code:

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

I'm assuming something is wrong with the "+=" but I have no idea what. I'm fairly certain that the area is correctly being returned. Help please. :)

Neil A.

Two things I noticed:

1. promptRaindrops returns a tuple

I am sure you didn't intend this, but when you say area += promptRaindrops(raindrops - 1), you are adding a tuple to area, which is an integer. To fix this, you should say area += promptRaindrops(raindrops - 1)[2] to get the area returned. However, your error is generated by

2. Your base case doesn't return a value

In promptRaindrops, you return a recursive call of the function whenever 1 <= raindrops <= 100. But, when it is outside that range, it returns nothing, only prints a message. Your function will always be outside of that range, because if you keep decreasing the value passed in to promptRaindrops, it will eventually go below 1. When it does, you return None (since you didn't return anything). That None bubbles up through every single recursion call made to that point, and you will inevitably be adding None to area. Add a return statement returning a tuple, and your error should vanish.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

From Dev

TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

From Dev

Unsupported operand types 'NoneType'

From Dev

TypeError: unsupported operand int and NoneType? Python

From Dev

Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

From Dev

Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

From Dev

unsupported operand types for * : 'float' and 'decimal'

From Dev

Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

From Dev

Unsupported operand type(s) for *: 'NoneType' and 'NoneType' (Python)

From Dev

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

From Dev

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Python 3: unsupported operand type(s) for /: 'float' and 'bytes'

From Dev

TypeError: unsupported operand type in Python

From Dev

I keep getting a " TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType' "

From Dev

TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

From Dev

TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

From Dev

TypeError: unsupported operand type(s) for -: 'float' and 'method'

From Dev

TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

From Dev

TypeError: unsupported operand type(s) for -: 'instance' and 'float'

From Dev

TypeError: unsupported operand type(s) for <<: 'int' and 'float'

From Dev

TypeError: unsupported operand type(s) for +: 'Timestamp' and 'float'

From Dev

Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

From Dev

Keras reports TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

From Dev

TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' with random generator?

Related Related

  1. 1

    Python TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

  2. 2

    TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

  3. 3

    Unsupported operand types 'NoneType'

  4. 4

    TypeError: unsupported operand int and NoneType? Python

  5. 5

    Python error: unsupported operand type(s) for -: 'float' and 'NoneType'

  6. 6

    Python TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

  7. 7

    unsupported operand types for * : 'float' and 'decimal'

  8. 8

    Python TypeError: unsupported operand type(s) for ^: 'float' and 'int'

  9. 9

    Unsupported operand type(s) for *: 'NoneType' and 'NoneType' (Python)

  10. 10

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

  11. 11

    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

    Python 3: unsupported operand type(s) for /: 'float' and 'bytes'

  18. 18

    TypeError: unsupported operand type in Python

  19. 19

    I keep getting a " TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType' "

  20. 20

    TypeError: unsupported operand type(s) for +: 'float' and 'tuple'

  21. 21

    TypeError: unsupported operand type(s) for +: 'decimal' and 'float'

  22. 22

    TypeError: unsupported operand type(s) for -: 'float' and 'method'

  23. 23

    TypeError: unsupported operand type(s) for /: 'float' and 'instancemethod'

  24. 24

    TypeError: unsupported operand type(s) for -: 'instance' and 'float'

  25. 25

    TypeError: unsupported operand type(s) for <<: 'int' and 'float'

  26. 26

    TypeError: unsupported operand type(s) for +: 'Timestamp' and 'float'

  27. 27

    Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

  28. 28

    Keras reports TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

  29. 29

    TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple' with random generator?

HotTag

Archive