-1

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)
4

2 回答 2

2

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
于 2014-03-14T21:34:23.173 回答
1

如果没有返回, Python 函数None默认返回。您有一个else调用函数的子句,但对它不执行任何操作,并且函数在此结束,因此如果它沿着该控制流路径运行,您None将从该函数返回。

于 2014-03-14T21:36:44.030 回答