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

Question:

#import the random package
import random
# This is to check wheter number is  negative or not.
def non_zero(data):
    try:
        while True:
            data1 = int(input(data))
            if data1 <= 0:
                pass
            else:
                break
    except ValueError:
        non_zero(data)
    else:
        print("else blockdata:",data1)
        return data1


#This function will verify the guess and random number
def guess(gue_num,ran_num):
    while  True:
        if gue_num < ran_num:
            print("Too small!")
            gue_num = non_zero("Guess: ")
        elif gue_num > ran_num:
            print("Too large!")
            gue_num = non_zero("Guess: ")
        else:
            print("Just Right!")
            break


inp_data = non_zero("Level: ")
ran_data = random.randint(1,inp_data)
gue_data = non_zero("Guess: ")
guess(gue_data,ran_data)

For the above program, I am giving the input "cat", again the prompt will ask for the level then i am giving the integer value. its throwing the type error. please help me out.

Asked By: sandeep maganti

||

Answers:

Because function didn’t give any value for return when function throw error
So ‘data1’ is None
Check line 13

#import the random package
import random
# This is to check wheter number is  negative or not.
def non_zero(data):
    try:
        while True:
            data1 = int(input(data))
            if data1 <= 0:
                pass
            else:
                break
    except ValueError:
        non_zero(data)  # <<- Here the function didn't return any value
    else:
        print("else blockdata:",data1)
        return data1


#This function will verify the guess and random number
def guess(gue_num,ran_num):
    while  True:
        if gue_num < ran_num:
            print("Too small!")
            gue_num = non_zero("Guess: ")
        elif gue_num > ran_num:
            print("Too large!")
            gue_num = non_zero("Guess: ")
        else:
            print("Just Right!")
            break


inp_data = non_zero("Level: ")
ran_data = random.randint(1,inp_data)
gue_data = non_zero("Guess: ")
guess(gue_data,ran_data)

if you want to fix this error then this is simple example

def non_zero(data):
    try:
        while True:
            data1 = int(input(data))
            if data1 <= 0:
                pass
            else:
                break
    except ValueError:
        data1 = non_zero(data)
    print("else blockdata:", data1)
    return data1
Answered By: dhkim

it is because the first input was return as None,
change your code to this one:

#import the random package
import random
# This is to check wheter number is  negative or not.
def non_zero(data):
    try:
        while True:
            data1 = int(input(data))
            if data1 <= 0:
                pass
            else:
                break
    except ValueError:
        print ('input int and not 0 value')
    else:
        print("else blockdata:",data1)
        return data1


#This function will verify the guess and random number
def guess(gue_num,ran_num):
    while  True:
        if gue_num < ran_num:
            print("Too small!")
            gue_num = non_zero("Guess: ")
        elif gue_num > ran_num:
            print("Too large!")
            gue_num = non_zero("Guess: ")
        else:
            print("Just Right!")
            break


inp_data = non_zero("Level: ")
while True:
    if not inp_data:
        inp_data = non_zero("Level: ")
    else:
        ran_data = random.randint(1,inp_data)
        gue_data = non_zero("Guess: ")
        guess(gue_data,ran_data)
        break
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.