类型错误:不支持的操作数类型Int和NoneType

时间:2014-03-14 21:31:16

标签: python operand

嘿伙计我正在研究一个python程序而且我一直在从循环返回错误,这应该只是重新提示用户输入一个数字。我遇到的问题是它不断返回一个非类型,它不能用于操作我需要做的其他函数,任何帮助是值得赞赏的。谢谢。

(这是我的代码,如果格式不正确,请提前抱歉。)

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)

2 个答案:

答案 0 :(得分:2)

您没有在getTickets(limit)区块内返回else

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

答案 1 :(得分:1)

如果没有返回任何内容,Python函数默认返回None。你有一个else子句来调用一个函数,但是没有对它执行任何操作,并且函数在那里结束,因此如果它向下控制流路径,你将从该函数返回None