当前位置: 动力学知识库 > 问答 > 编程问答 >

python - What is wrong with this function? Can't figure it out

问题描述:

I have been trying to get a function to work and I just cant seem to figure out why I get

TypeError: stringCounter() takes exactly 1 argument (0 given)

Everytime I run this code

def stringCounter(count):

name ='Hi'

formatString = "%s run count is %d"

print formatString % (name, count)

test = stringCounter()

test.stringCounter(1)

I have tried adding self, messing with the variables passed what am I doing wrong? Thanks guys!

网友答案:

This:

test = stringCounter()

is obviously wrong, it calls the stringCounter() function without passing any arguments. Just as the error says. Just delete this line.

And this:

test.stringCounter(1)

is also wrong, there is no thing called test that contains a function called stringCounter() in your program. It should just be:

stringCounter(1)
网友答案:

The error is self-explanatory:

test = stringCounter()

You're not passing any arguments to stringCounter() here, when one (namely count) is expected. You likely simply want

stringCounter(1)  # don't assign this to anything, it returns None
网友答案:

Do this:

test = stringCounter

test(1)

Or directly:

stringCounter(1)
网友答案:

You are calling stringCounter with no arguments in the line

test = stringCounter()

it looks like you are trying to instantiate it, but it is a function, not a class.

网友答案:

You're not passing any parameters to the function test = stringCounter()

And this line doesn't make sense since test is None (the previous function returns nothing)

test.stringCounter(1)
分享给朋友:
您可能感兴趣的文章:
随机阅读: