【问题标题】:Type Error: Unsupported operand types Int and NoneType [closed]类型错误:不支持的操作数类型 Int 和 NoneType [关闭]
【发布时间】:2014-04-20 09:34:42
【问题描述】:

大家好,我正在开发一个 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)

【问题讨论】:

标签: python operand


【解决方案1】:

您没有在 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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-03-16
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多