user187232
user187232

Reputation: 58

TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

this unfinished piece of code is intended to return the sum of of letters if 1 to x were written out in words:

#!/usr/bin/python
def Base1(n):
  if (n==1):
      return len('one') 
  if (n==2):
    return len('two') 
  if (n==3):
      return len('three')
  if (n==4):
      return len('four') 
  if (n==5):
      return len('five') 
  if (n==6):
      return len('six') 
  if (n==7):
      return len('seven') 
  if (n==8):
      return len('eight')
  if (n==9):
      return len('nine')
def Base10(g):
  if (g == 10):
    return len('ten')
  if (g == 11):
    return len('eleven')
  if (g == 12):
    return len('twelve')
  if (g == 13):
    return len('thirteen')
  if (g == 14):
    return len('fourteen')
  if (g == 15):
    return len('fifteen')
  if (g == 16):
    return len('sixteen')
  if (g == 17):
    return len('seventeen')
  if (g == 18):
    return len('eightteen')
  if (g == 19):
    return len('nineteen')
  if (g == 20):
    return len('twenty')
  if (g > 20):
    return BiggerThan20(g)
def BiggerThan20(t):
  if t<30:
    return len('twenty')+Base1(t-20)
  if t<40:
    return len('twenty')+Base1(t-20)


def trial(runcounter):
  summitup = 0
  if (len(str(runcounter))==1):
    summitup += Base1(runcounter)
  if (len(str(runcounter))==2):
    summitup += Base10(runcounter)
  return summitup

lettersum = 0

start = int(input("Please enter an integer: "))
for k in range(1, start+1, 1):
  lettersum += trial(k)
 #print (k, lettersum)
print(lettersum)

It runs fine up to 29, but if I enter anything >= 30 bash returns following output:

opq@home:/home/opq/python pe17.py

Please enter an integer: 32

Traceback (most recent call last):
  File "pe17.py", line 65, in <module>
    lettersum += trial(k)
  File "pe17.py", line 58, in trial
    summitup += Base10(runcounter)
  File "pe17.py", line 45, in Base10
    return BiggerThan20(g)
  File "pe17.py", line 50, in BiggerThan20
    return len('twenty')+Base1(t-20)
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

I am new to programming and unsure why this error occurs.

Base1 seems suddenly to return None when the first if-statement in BiggerThan20 turns false.

I appreciate any help you may provide.

Solved! I forgot to care for the tens... Thank you for your help

Upvotes: 2

Views: 5205

Answers (2)

falsetru is correct.

perhaps you want this:

if t<40:
return len('twenty')+Base1(t-20)

to be:

if t<40:
return len('thirty')+Base1(t-30)

Upvotes: 0

falsetru
falsetru

Reputation: 369444

Base1 does not return anything for number 10 or greater number.

>>> Base1(1)
3
>>> Base1(10) # None
>>> Base1(20) # None

>>> len('twenty') + Base1(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Base10, BiggerThan20 have same problem.

  • Base10 does not return for numbers less than 10.
  • BiggerThan20 does not return for number >= 40.

Upvotes: 1

Related Questions