如何获得函数以返回引用以外的内容?

时间:2019-02-16 16:48:28

标签: python-3.x function

运行此函数时,它将返回None和一个引用,而不是预期值。

我已经在使用“ return”了,它应该使函数返回预期的值,但是就像我说的那样,它仍在返回引用。

def querydate():
    querydate = int(input("Please enter a year between 2000 and 2017 for 
    processing injury data "))

    numberchoice0 = querydate
    while numberchoice0 is querydate:
        try:
            while int(numberchoice0)<2000:
                print("oopsie, that year is before those for which this 
                       query searches.")
                quit()
            while int(numberchoice0)>2017:
                print("oopsie, that year is after those for which this 
                       query searches.")
                quit()
        except ValueError:
            print ('That was not an integer!')
            affirmations = ('YES', 'Y')
            answer = input("Do you want to continue? (Yes/Y/y):\n")
            if answer.strip().upper() in affirmations:
                continue
        else:
            return querydate()

print(querydate())

def verify():
    verify = input("please enter 'yes' or 'no' ")
    if verify == "no":
        print("You should be more careful about inputting data!")
        quit()
    while verify != "yes":
        print(verify, "is not an appropriate input. If you answered 'YES' 
              or 'Yes' please enter 'yes'")
        continue
    if verify == "yes":
        print("Great! Let us continue")

verify()

我期望输出为2000到2017年之间的数字,但是当我打印querydate()时,它将返回“ None”,而当我使用querydate()引用verify()时它实际上返回<function querydate at 0x000001F1DCFB9A60>

1 个答案:

答案 0 :(得分:2)

return不会使函数返回预期值,必须根据要返回的想要明确指定它。

您希望将输出从2000发送到2017,因此您需要返回返回此值的值。

def querydate():
    qDate = int(input("Please enter a year between 2000 and 2017 for 
    processing injury data "))

    numberchoice0 = qDate
    while numberchoice0 is qDate:
        try:
            while int(numberchoice0)<2000:
                print("oopsie, that year is before those for which this 
                       query searches.")
                quit()
            while int(numberchoice0)>2017:
                print("oopsie, that year is after those for which this 
                       query searches.")
                quit()
        except ValueError:
            print ('That was not an integer!')
            affirmations = ('YES', 'Y')
            answer = input("Do you want to continue? (Yes/Y/y):\n")
            if answer.strip().upper() in affirmations:
                continue
        else:
            return qDate #returning the integer instead of None

print(querydate())

def verify():
    verify = input("please enter 'yes' or 'no' ")
    if verify == "no":
        print("You should be more careful about inputting data!")
        quit()
    while verify != "yes":
        print(verify, "is not an appropriate input. If you answered 'YES' 
              or 'Yes' please enter 'yes'")
        continue
    if verify == "yes":
        print("Great! Let us continue")

verify()

另外,由于您未明确返回任何内容,因此用querydate()引用verify()应该返回地址引用,但是如果返回的是整数querydate或{ {1}},然后返回范围numberchoice0的一年。

编辑:

就您的2000-2017而言,它的发生是由于局部变量函数名称的命名相同。因此,首先标识符TypeError: 'int' object is not callable指的是函数 querydate,然后它进入函数内部,现在它指的是变量 {{1} },并且在遇到分配变量querydate()时不再引用功能。因此,更改标识符之一的名称可以解决此问题。