1

I've never programmed in Python before, and I've taken some examples of the internet and edited them a bit to create a code that finds prime numbers. However, even after consulting people who know quite a bit of Python, I can't figure out what the problem with this code is. I always just get the error:

TypeError: 'str' object is not callable

for this line:

      if (j > i/j) : print(i, " is prime")

Here's the full code for the program.

i = 2
while(i < 100):
   j = 2
   while(j <= (i/j)):
      if not(i%j): break
      j = j + 1
      if (j > i/j) : print(i, " is prime")
   i = i + 1
Felix
  • 21
  • 1
  • 2
  • 4
    You appear to have assigned a string to the name `print`. – Martijn Pieters Jun 15 '16 at 08:48
  • The dupe [here](http://stackoverflow.com/questions/30591182/how-to-fix-redefinition-of-str?lq=1) refers to overshadowing of the `str` builtin, It's the same case with `print`. Refer to Martijn's answer to solve your *exact* problem and the dupe to gain some insight. – Bhargav Rao Jun 15 '16 at 09:36

1 Answers1

2

Elsewhere in your code or session, you bound a string object to the name print:

print = 'foo'

Now print(...) is trying to call a string instead of the built-in function.

In an interactive session, delete the name print:

del print

In a script, make sure you don't mask the name print; pick a different name for that variable.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343