Str object not callable


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#


So i'm trying to find a prime number. And my code works fine. But I'm just trying to get my code to print out with the %s format that my number is not prime.This is more of a conceptual question.

Error message below:
2
Traceback (most recent call last):
File "python", line 17, in
File "python", line 10, in is_prime
TypeError: 'str' object is not callable


I would've expected it to print: "Darn x is not a prime number" where x is the number I'm checking is prime. See code below.


def is_prime(x):
    n=2
    if x <2:
        return False
    else:
        word = str(x)
        while n <= x-1:
            print n
            if x % n == 0:
                print "Darn %s is not a prime number"(word)
                return False
                break
            n += 1
        else: 
            print "Yay"
            return True
print is_prime(4)


#2

I guess I was just confused because here is the update that works:

def is_prime(x):
n=2
if x <2:
return False
else:
while n <= x-1:
print n
if x % n == 0:
print "Darn %s is not a prime number" % (x)
return False
break
n += 1
else:
print "Yay"
return True
print is_prime(4)

But in the glossary they show it as:

return "%s race horse weighing %f and wild status is %b" (self.color,self.weight,self.wild)
link:https://www.codecademy.com/articles/glossary-python
Is that a typo? If not why does theirs work with out the extra % but mine fails?


#3

One would expect so. The modulo parameter indicator is required in the formatting syntax.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.