Python gives me an error when I try to turn an input into an integer
I wanted to make a program in Python3 that would sum up the digits of a given number. I came up with this:
x = 68328
sum=0
for digit in str(x):
sum=sum int(digit)
print(sum)
But I wanted to x variable to be an input. So I did this:
x = int(input)
sum=0
for digit in str(x):
sum=sum int(digit)
print(sum)
Unfortunately, now the program stumbles upon an error:
Traceback (most recent call last):
File "/tmp/tempCodeRunnerFile.python", line 1, in <module>
x = int(input)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'builtin_function_or_method'
I dont know what to do. Ive written programs using variable = int(input) before and they worked fine.
CodePudding user response:
You missed the parentheses: use int(input())
instead.
input
itself is a function, which for sure can not be converted into an integer. You need to invoke it, input()
, and use its return value.
CodePudding user response:
you can try: x = int(input())
.
input()
can accept input, but input
is only a python command.