Nick_Clements7
Nick_Clements7

Reputation: 21

Trying to write code but getting TypeError: unsupported operand type(s) for +: 'float' and 'type'

This is the code I'm printing out but am getting a error. TypeError: unsupported operand type(s) for +: 'float' and 'type'

import math

def circles_of_radius():
    #Prints out circumference and area with the ranged radius
     for x in range(3,7):
         C = (2 * math.pi * x) #calculates circumference
         A = (math.pi * x**2) #calculates area
         print(("A circle with radius", x) +str ("has circumference", C) +str (" and area", A))

Upvotes: 0

Views: 792

Answers (3)

norie
norie

Reputation: 9857

You should only be passing the numeric values to str.

print("A circle with radius "+str(x)+" has circumference "+str(C)+" and area "+str(A))

However, it would probably be better/easier to use a f-string.

print(f"A circle with radius {x} has circumference {C} and area {A}")

You might want to round the circumference and area too.

 print(f"A circle with radius {x} has circumference {round(C,2)} and area {round(A,2)}")

Upvotes: 2

Mario Khoury
Mario Khoury

Reputation: 392

This is you code:

import math
def circles_of_radius():
    for x in range(3,7):
        C = (2 * math.pi * x)
        A = (math.pi * x**2)
        print(("A circle with radius",x) + str("has circumference",X) + str (" and area", A))

I would suggest to replace the print by that:(Note that , will add a space to the print)

print("A circle with radius" , str(x), "has circumference", str(X), "and area", str(A))

or (Note that using "+" you will need to add the space before and after your message)

    print("A circle with radius " + str(x) + " has circumference " + str(X) + " and area "+ str(A))

in your case you are adding a float to a string and then converting it to a string with the method str()

Upvotes: 2

Rima
Rima

Reputation: 1455

You can use F string to print value.

print(f"A circle with radius {x} has circumference {C} and area {A}")

output

A circle with radius3has circumference 18.84955592153876 and area 28.274333882308138
A circle with radius4has circumference 25.132741228718345 and area 50.26548245743669
A circle with radius5has circumference 31.41592653589793 and area 78.53981633974483
A circle with radius6has circumference 37.69911184307752 and area 113.09733552923255

Upvotes: 2

Related Questions