0
def input():
    h = eval(input("Enter hours worked: \n"))
    return h

def main():
    hours = input()
    print(hours)  
main()

As you can tell, I'm new to Python. I keep getting: "TypeError: input() takes exactly 1 argument (0 given)." Any help/explanation would be greatly appreciated -- thank you all very much!

3
  • 1
    input is a method in python and it takes 1 argument. try to use another name for your function def input() Commented Jun 1, 2012 at 4:24
  • Thank you islandmyth -- that was it. I renamed the function and all is well. Commented Jun 1, 2012 at 4:27
  • 1
    Don't use eval() for this. int() or float() will probably work for you. eval() is extremely dangerous to use with user input. eg try entering help(__import__('os').removedirs) Commented Jun 1, 2012 at 4:46

5 Answers 5

2

You define a function called input in the first line that takes zero arguments and then when you call the input function later (which I assume you intended it to call the one that comes with Python and may have accidentally overridden) you pass it one variable.

# don't try to override the buil-in function
def input_custom():
    h = eval(input("Enter hours worked: \n"))
    return h

def main():
    hours = input_custom()
    print(hours)  
main()
Sign up to request clarification or add additional context in comments.

Comments

1

input() is the name of a builtin Python function.

In your code, you override it, which is definitely not a good idea. Try naming your function something else:

def get_hours():
    h = eval(input("Enter hours worked: \n"))
    return h

def main():
    hours = get_hours()
    print(hours)  

main()

1 Comment

This depends on the Python version, but the OP appears to be using print as a function, which implies Py3. But, more importantly, this doesn't explain the particular error the OP is getting.
1

Change your input function with a different name since input is a method in python.

def inputx():
     h = eval(input("Enter hours worked: \n"))
     return h

 def main():
     hours = inputx()
     print(hours)

 main()

Comments

1

I can't replicate your exact error - instead I get:

TypeError: input() takes no arguments (1 given)

But, your error is likely caused by the same thing - when you name your function input, you shadow the built-in input: Python can't see both, even though yours doesn't expect a prompt. If you name yours myinput instead, Python can tell the difference:

def myinput():
    h = eval(input("Enter hours worked: \n"))
    return h

def main():
    hours = myinput()
    print(hours)  
main()

Comments

0

Other answers have covered a lot.. I would just like add some thoughts to it. First of all your function name input is overriding the python builtin function

so first of all

def my_input():
    return input("Enter hours worked: ")
print my_input()
    

This should suffice.

Concept:

Now if you are using Python 2.X version then there is no need for eval.

input(): Python by default evaluates the input expression if it is recognized by python.

raw_input(): Here the input is taken as a string which needs to be evaluated.

In case of Python3.x:

input() behaves like raw_input() and raw_input() is removed.

So your code should be

def my_input():
    return float(input("Enter hours worked: "))
print(my_input())

It is safer and better way to take inputs and also tells us why eval is not recommended.

You don't know what's gonna come through that door.

Thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.