Why I’m getting an error


#1

Hi everyone,

So I have added some additional function to digit_sum function, but I need some help with trying to work out why I’m getting an error. The entire function works except for checking whether the entered value is a string. I will include only the code I am asking about, and not the entire function, for brevity.

def digit_sum(n):
if type(n) is str:
return “Input is a string”

print digit_sum(“frog”)

The above doesn’t work, but below does:
n = “frog”
if type(n) is str:
print True

Is this because in the latter I have defined n as a variable and in the former situation it is a value? Has it got something to do with the function?

Thanks for your time in advance.


How can I break down these problems into easier parts?
#2
>>> def foo(n):
	if type(n) is str:
		return "Yay"
	else:
		return "Nay"

	
>>> print (foo('bar'))
Yay
>>> print (foo(1234))
Nay
>>> 

As we can see it works fine. Can’t tell if there is an indentation issue in your code.


#3

This is my full code, and it gets stuck with the elif part.

def digit_sum(n):
  if n % 1 != 0 and n > 0:
    return "Input is not a positive integer"
  elif type(n) is str:
    return "Input is a string"
  sum = 0
  for i in str(n):
      sum += int(i) 
  return sum


#4

Should that be, and n < 0?


#5

You’re right, yes. But that’s not why the string is giving an error?


#6
>>> digit_sum('foo')
Traceback (most recent call last):
  File "<pyshell#238>", line 1, in <module>
    digit_sum('foo')
  File "<pyshell#237>", line 2, in digit_sum
    if n % 1 != 0 and n < 0:
TypeError: not all arguments converted during string formatting
>>> 

You should probably check for a string first so that line doesn’t raise an exception.

>>> def digit_sum(n):
  if type(n) is str:
    return "Input is a string"
  elif n % 1 != 0 and n < 0:
    return "Input is not a positive integer"
  sum = 0
  for i in str(n):
      sum += int(i)
  return sum

>>> digit_sum('foo')
'Input is a string'
>>> digit_sum(1234)
10
>>> digit_sum(-1234)
Traceback (most recent call last):
  File "<pyshell#243>", line 1, in <module>
    digit_sum(-1234)
  File "<pyshell#240>", line 8, in digit_sum
    sum += int(i)
ValueError: invalid literal for int() with base 10: '-'

Change and to or

>>> def digit_sum(n):
  if type(n) is str:
    return "Input is a string"
  elif n % 1 != 0 or n < 0:
    return "Input is not a positive integer"
  sum = 0
  for i in str(n):
      sum += int(i)
  return sum

>>> digit_sum(-1234)
'Input is not a positive integer'
>>> 

#7

Thank you! Can I ask why an exception is raised when I ask to check for the string second?


#8

We can’t run a numeric test on a string.


#9

Hm…I’m not sure I understand…if I first ask “is this a positive integer”, and then ask “if not, is this a string”, how am I running a numeric test on the string? Is it the case that I ask, “Is this a string”, and if it is it ends there, I don’t have a chance for the error to occur?


#10

If the value passed in is a string, then we cannot run a modulo operation on it. Test for a string, first, to be sure it does not get tested numerically.


#11

Oh okay, now I see. Thank you!