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

2024-06-09 06:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我正在开发一个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)

Tags: ofthereturnifdeffunctionthisprice
2条回答

如果不返回任何内容,Python函数默认返回None。您有一个else子句,它调用一个函数,但对它不做任何操作,并且函数结束于此,因此如果它沿着控制流路径走下去,您将从该函数返回None

您不会在else块中返回getTickets(limit)

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

相关问题 更多 >