0

人々が日付を入力できるように、私は小さなコードを書きました。1未満または12を超える月の入力を停止するエラーチェックは、これらの範囲内にある場合にのみ値を返す必要があります。「範囲外」の数値をいくつか入力すると、1か月間再入力するように正しく再要求されますが、すべての値が返されます。何が起こっている?

# 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()

結果は、正しい月「12」を入力する前に、悪い月「15」、「14」、および「13」を入力した結果です。

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

何が起こっている?

4

2 に答える 2

2

関数は何も返しません。質問関数を何度も呼び出していますが、戻り値を無視しています。

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

月が1より小さいか、12より大きい場合、関数は戻りNoneます(デフォルト)。次に、ステートメントupdatemonth = month_check(month)はに割り当てNoneられupdatemonthprint("Month q returning:", updatemonth)実行されると、を出力しMonth q returning: Noneます。ただし、month_q再帰的に呼び出したため、それは前の呼び出しに戻り、前のmonth_check()呼び出しに戻りますNone

これは、呼び出しmonth_qなどの関数のネストされたトレースです。month_checkmonth_q

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

代わりに、チェック関数が正しい結果を示すためにTrueまたはFalseを返すようにし、再入力の要求をに残しますmonth_q

whileただし、再帰ではなくループを使用してこれを実行します。十分な頻度で質問する必要がある場合は、再帰が深すぎる例外が発生します。

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

そのチェックをインライン化することもできます。

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:句を使用することは決して良い考えではありません。上記のコードでは、代わりに非整数値を入力したときにValueError発生する値をキャッチし、整数を入力したが1から12までの値ではなかった場合に同じ例外を発生させますこれにより、「1か月ではない」エラー処理が大幅に簡素化されます。int()

于 2013-01-21T12:13:12.653 に答える
0

これを試して:

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

それは間違った月の印刷を中断するはずです

また、変更する必要があります

updatemonth = month_check(month)
print("Month q returning:", updatemonth)
return updatemonth

に:

updatemonth = month_check(month)
try:
    updatemonth
except NameError:
    updatemonth = None

if updatemonth is None:
    break
print("Month q returning:", updatemonth)
return updatemonth
于 2013-01-21T12:12:14.107 に答える