首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python 3中单变量的意外多个返回

Python 3中单变量的意外多个返回
EN

Stack Overflow用户
提问于 2013-01-21 20:07:18
回答 2查看 90关注 0票数 0

我已经写了一些代码,以便人们可以输入日期。如果错误检查停止输入的月份小于1或大于12,则仅当该值在这些范围内时才应返回值。如果我输入了几个“越界”数字,它会正确地重新请求重新输入一个月,但返回所有的值。怎么一回事?

代码语言:javascript
复制
# the question asked to get the month input for the xml updater
def month_q():
    try:
        month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
    except:
        print("That was not a valid number. Please re-enter a 2 digit month")
        month_q()
    updatemonth = month_check(month)
    print("Month q returning:", updatemonth)
    return updatemonth


# check the update month is a valid month
def month_check(month):
    if month < 1:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    elif month > 12:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    else:
        print("Month_check returning month:", month)
        return month



# this updates the xml file with the date of the last installer    
def xml_updater():
    updatemonth = month_q()
    print("Update month:", updatemonth)


xml_updater()

结果是输入坏的月份'15‘、'14’和'13‘,然后输入正确的月份'12’:

代码语言:javascript
复制
What was the month [MM] which the installers were updated/created by xxx?:15
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:14
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:13
The month must be a number between 01 and 12. Please re-enter
What was the month [MM] which the installers were updated/created by xxx?:12
Month_check returning month: 12
Month q returning: 12
Month q returning: None
Month q returning: None
Month q returning: None
Update month: None

怎么一回事?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-21 20:13:13

您的函数没有返回任何内容;它一次又一次地调用问题函数,但忽略了返回值:

代码语言:javascript
复制
def month_check(month):
    if month < 1:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    elif month > 12:
        print("The month must be a number between 01 and 12. Please re-enter")
        month_q()
    else:
        print("Month_check returning month:", month)
        return month

如果month小于1或大于12,该函数将返回None (默认值)。然后,语句updatemonth = month_check(month)None赋值给updatemonth,当print("Month q returning:", updatemonth)运行时,它将打印Month q returning: None。但由于您以递归方式调用了month_q,因此它将返回到前面的month_check()调用,该调用再次返回None,依此类推。

下面是您的函数的嵌套跟踪,因为month_q调用month_check调用month_q等:

代码语言:javascript
复制
month_q():
    What was the month [MM] which the installers were updated/created by xxx?:15
    month_check(15):
        The month must be a number between 01 and 12. Please re-enter
        month_q():
            What was the month [MM] which the installers were updated/created by xxx?:14
            month_check(15):
                The month must be a number between 01 and 12. Please re-enter
                month_q():
                    What was the month [MM] which the installers were updated/created by xxx?:13
                    month_check(13):
                        The month must be a number between 01 and 12. Please re-enter
                        month_q():
                            What was the month [MM] which the installers were updated/created by xxx?:12
                            month_check(12):
                                Month_check returning month: 12
                                <- 12
                            Month q returning: 12
                            <- 12
                        <- None
                    Month q returning: None
                    <- None
                <- None
            Month q returning: None
            <- None
        <- None
    Month q returning: None
    <- None
Update month: None

取而代之的是,让check函数返回True或False以指示正确的结果,并将要求重新输入的请求留给month_q

不过,使用while循环而不是递归可以做到这一点;如果您不得不频繁地询问,您最终会得到一个递归太深的异常。

代码语言:javascript
复制
def month_q():
    while True:
        try:
            month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
            if not month_check(month):
                raise ValueError()
        except ValueError:
            print("Please re-enter a 2 digit month")
            continue
        print("Month q returning:", month)
        return month


# check the update month is a valid month
def month_check(month):
    if month < 1 or month > 12:
        print("The month must be a number between 01 and 12")
        return False
    return True

您也可以内联该检查:

代码语言:javascript
复制
def month_q():
    while True:
        try:
            month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
            if month < 1 or month > 12:
                raise ValueError()
        except ValueError:
            print("Please re-enter a 2 digit month between 01 and 12.")
            continue
        print("Month q returning:", month)
        return month

使用except:子句从来都不是一个好主意;在上面的代码中,当您输入一个非整数值时,我捕获了int()引发的ValueError,如果您确实输入了一个整数,但它不是介于1和12之间(包括1和12)的值,则会引发相同的异常。这大大简化了“不是一个月”的错误处理。

票数 2
EN

Stack Overflow用户

发布于 2013-01-21 20:12:14

试试这个:

代码语言:javascript
复制
try:
    month = int(input('What was the month [MM] which the installers were  updated/created by xxx?:'))
except:
    print("That was not a valid number. Please re-enter a 2 digit month")
    month_q()
    break

这应该会中断错误月份的打印

另外,你应该改变

代码语言:javascript
复制
updatemonth = month_check(month)
print("Month q returning:", updatemonth)
return updatemonth

至:

代码语言:javascript
复制
updatemonth = month_check(month)
try:
    updatemonth
except NameError:
    updatemonth = None

if updatemonth is None:
    break
print("Month q returning:", updatemonth)
return updatemonth
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14438259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档