-1
def triangular(n):
    tri = 0
    for i in range(1, n+1):
        tri = tri + i

    print ("The answer is equal to" +(tri))

triangular(4)

I just need help with the print statement because it doesn't work. I am trying to print the answer is equal to tri

user3204229
  • 75
  • 1
  • 9

2 Answers2

7
print("The answer is equal to", tri)

or

print("The answer is equal to %i"%tri)

or

print("The answer is equal to {}".format(tri))

The docs also have some more ways to do this.

wflynny
  • 18,065
  • 5
  • 46
  • 67
2

You need to cast your int to a str.

# str is optional here because print will call str on its arguments for you
print("The answer is equal to", str(i)) 

or

# str is not optional here because you are concatenating
print("The answer is equal to " + str(i))
senshin
  • 10,022
  • 7
  • 46
  • 59