Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What should I do to fix this error?
"int object is not callable"




<pre>def isTriangular(x):
    """Returns whether or not a given number x is triangular.
    
    The triangular number Tn is a number that can be represented in the form of a triangular 
    grid of points where the first row contains a single element and each subsequent row contains 
    one more element than the previous one.
    
    We can just use the fact that the nth triangular number can be found by using a formula: Tn = n(n + 1) / 2.
    
    Example: 3 is triangular since 3 = 2(3) / 2
    3 --> 2nd position: (2 * 3 / 2)
    
    Example: 15 is triangular since 15 = 5(6) / 2
    15 --> 5th position: (5 * 6 / 2)
    """
    
    # your code here
    i = 0
    for i in range(1 + i, x + 1):
        triangle = i(i + 1) / 2
        if triangle == x:
            return True
        elif i >= triangle:
            return False
        
isTriangular(3)



What I have tried:

I have tried to concatenate the integer but it's not possible. It should have substituted the values of i and applied it to the expression but I don't have any clue what the error means...
Posted
Updated 29-Aug-21 2:04am
v2

The problem is that you break out of the loop as soon as you find a value that is not triangular. So for the input value 3 you break out when the loop count reaches 2. You need to continue the loop until you find a match, or the loop terminates. So set a flag at the beginning which you can use after the loop terminates to check if you found the number. something like:
Python
def isTriangular(x):
    found = False # assume no find
    for i in range(1, x + 1):
        triangle = i * (i + 1) / 2
        if triangle == x:
            found = True
            break # terminate if the number is found

    return found 
 
Share this answer
 
Comments
Mr. Expert 2021 29-Aug-21 8:09am    
Got it thanks man
Mr. Expert 2021 29-Aug-21 8:10am    
When I enter a non-triangular number. It doesn't print anything.

def isTriangular(x):
"""Returns whether or not a given number x is triangular.

The triangular number Tn is a number that can be represented in the form of a triangular
grid of points where the first row contains a single element and each subsequent row contains
one more element than the previous one.

We can just use the fact that the nth triangular number can be found by using a formula: Tn = n(n + 1) / 2.

Example: 3 is triangular since 3 = 2(3) / 2
3 --> 2nd position: (2 * 3 / 2)

Example: 15 is triangular since 15 = 5(6) / 2
15 --> 5th position: (5 * 6 / 2)
"""

# your code here
i = 0
for i in range(1 + i, x + 1):
triangle = i * (i + 1) / 2
if triangle == x:
print("True")
elif i > triangle:
print("False")


isTriangular(24)
Richard MacCutchan 29-Aug-21 8:16am    
Yes because you have changed from the working solution I gave you.
Mr. Expert 2021 29-Aug-21 8:29am    
Even with your code it's not outputting anything
Richard MacCutchan 29-Aug-21 8:47am    
No, because it is returning the values, True or False. If you want it to print something the change the last line to:
if found:
    print("True")
else:
    print("False")

Try and think carefully what you want the code to do at each step.
Not sure if I understand your question correctly but if your trying to multiply, you're missing the asterisk.
So instead of
triangle = i(i + 1) / 2

try
triangle =  i * (i + 1) / 2
 
Share this answer
 
Comments
Mr. Expert 2021 29-Aug-21 3:28am    
Alright that worked but when I enter 3 as my triangular number the output should be True not False. What can I do to make that happen?
Wendelius 29-Aug-21 3:42am    
Try using the debugger. When you run the code line-by-line you'll notice that you enter the for loop only once, the value of i is 1 and the result for triangle is 1. So in the first iteration you compare 1 (i) to 3 (x) which is not equivalent and the program returns 3.

Having that said, you need to reconsider the loop so that you test all relevant positions and once done you decide if the number is triangular or not
Mr. Expert 2021 29-Aug-21 3:51am    
Is it possible that you can provide me the code for that?
Wendelius 29-Aug-21 3:55am    
In order to get to know a language, it's important that you try and experiment what happens with your code and when you change it.

For example try using the debugger and remove the elif section. Now when debugging, investigate the variables and see what happens.
Mr. Expert 2021 29-Aug-21 4:01am    
Alright then.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900