简体   繁体   中英

How can I use .count function correctly?

代码图像

a=name11.count("t")+name11.count("r")+name11.count("u")+name11.count("e")
b=name21.count("l")+name21.count("o")+name21.count("v")+name21.count("e")

I can't see what's wrong here, but when I run the code it give attribute error. I tried swapping lines and looked for typos but there is none I saw.

did you mean like this

print('welcome to the Love calculater')
name1=input('what is your name?\n')
name2=input('what is their name?\n')
name11=name1.lower()
name21=name2.lower()
a=name11.count("t")+name11.count("r")+name11.count("u")+name11.count("e")
b=name21.count("l")+name21.count("o")+name21.count("v")+name21.count("e")
....

is you?

When you call a method/function without () , python don't call the actual method but it returns a function reference

in line number 8 & 9 you missed to call the method lower() but you assigned method reference of str.lower which means name11 is not a str so the object don't have any method called count()

name  = "ABC".lower
print(name)
#op: <built-in method lower of str object at 0x7a1e3f1420>
print(name())
#op: abc

As you can see in this example name is actually a reference, however you can invoke the method by adding ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM