TypeError: not all arguments converted during string formatting

Hi all,

I’m very new to python and have no programming background. I’m currently working on the following code to sum up 10 numbers:

num = input("Enter 10 numbers:")

sum = 0
while(num!=0):
  remainder = int(num)%10
  sum=sum+remainder
  num=num//10
print("The sum of the number is:", sum)

I always get the error: TypeError: not all arguments converted during string formatting.

Could anybody help me? Any tips are appreciated!

Depending on the version of Python that you’re using, try changing your print statement to one of the following:

print 'The sum of the number is: ${0}'.format(sum)

or

print 'The sum of the number is: %d'. % sum

Check the responses here for more info: https://stackoverflow.com/questions/18053500/typeerror-not-all-arguments-converted-during-string-formatting-python

I think the error has to do with the first line of code num should have an integer or float.

Try this instead;

num = int(input("Enter a number: "))

if num < 0:
   print("Enter a positive number")
else:
   sum = 0
  
   while(num > 0):
       sum += num
       num -= 1
   print("The sum of the number is:", sum)

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