-->

Type Error: Unsupported operand types Int and None

2019-08-13 12:12发布

问题:

Hey guys I am working on a python program and I keep getting errors returned from the loop which is supposed to just reprompt the user to enter a number. The problem I am having is that it keeps returning a nonetype which cannot be used to operate on which I need to do in on of the other functions any help is appreciated. Thanks.

( Here's my code, Sorry ahead of time if it is not formatted correctly. )

def getTickets(limit):
   ticketSold=int(input("How many tickets were sold? "))
   if (ticketsValid(ticketSold,limit)):
        return ticketSold
   else:
        getTickets(limit)

#This function checks to make sure that the sold tickets are within the Limit of seats
def ticketsValid(sold,limit):

    if (sold>limit or sold<0):
        print ("ERROR: There must be tickets less than "+str(limit)+" and more than 0")
        return False
    return True
# This function calculates the price of the tickets sold in the section.
def calcIncome(ticketSold,price):
    return ticketSold*(price)

回答1:

You are not returning getTickets(limit) inside your else block:

def getTickets(limit):
   ticketSold=int(input("How many tickets were sold? "))
   if (ticketsValid(ticketSold,limit)):
        return ticketSold
   else:
        return getTickets(limit)  # You need to use return here


回答2:

Python functions return None by default, if nothing is returned. You have an else clause that calls a function, but does nothing with it, and the function ends there, therefore if it goes down that control flow path, you will return None from that function.