You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python列表仅存储初始输入问题:如何保存验证后的有效输入?

问题:如何让列表存储验证后的有效输入?

当前制作过往购买记录列表时,存在输入验证后列表仍存储初始无效值的问题:比如输入无效内容被提示重新输入并输入有效内容后,列表里还是存第一次的无效值。举个例子,输入月份为33,验证不通过后重新输入5,但列表最终存储的还是33。

原验证函数代码:

import time

def check_if_valid_number(item_being_tested, Error_Message1, Maximum, Minimum):
    if item_being_tested.isnumeric() == False:
        while item_being_tested.isnumeric() == False:
            print(Error_Message1)
            time.sleep(.3)
            print("Please try again:")
            item_being_tested = input()
    int_number_value = int(item_being_tested)
    if int_number_value > Maximum or int_number_value < Minimum:
        while int(item_being_tested) > Maximum or int(item_being_tested) < Minimum:
            print(Error_Message1)
            time.sleep(.3)
            print("Please try again:")
            item_being_tested = input()
            if item_being_tested.isnumeric() == False:
                while item_being_tested.isnumeric() == False:
                    print(Error_Message1)
                    time.sleep(.3)
                    print("Please try again:")
                    item_being_tested = input()

原调用代码:

dates = []
month = input("month:")
check_if_valid_number(month, "Month must be between 1-12 and not include letters", 12, 1)
day = input("day:")
check_if_valid_number(day, "Day must be between 1-31 and not include letters", 31, 1)
year = input("year:")
check_if_valid_number(year, "Year cannot include letters", float('inf'), float('inf'))
dates.append("{}/{}/{}".format(month,day,year))

问题原因

验证函数内部修改了item_being_tested变量,但没有将最终的有效值返回给调用方。调用代码里的month、day、year始终是最初输入的原始值,并没有更新为验证后的有效内容,导致列表存储的还是初始输入。

解决方案

修改验证函数,让它返回最终通过验证的有效数字(字符串形式,保持输入格式),然后在调用时将变量赋值为函数的返回结果。

修改后的验证函数

import time

def check_if_valid_number(item_being_tested, Error_Message1, Maximum, Minimum):
    # 先检查是否为数字
    while not item_being_tested.isnumeric():
        print(Error_Message1)
        time.sleep(.3)
        print("Please try again:")
        item_being_tested = input()
    # 检查数值范围
    int_value = int(item_being_tested)
    while int_value > Maximum or int_value < Minimum:
        print(Error_Message1)
        time.sleep(.3)
        print("Please try again:")
        item_being_tested = input()
        # 重新输入后再次检查是否为数字
        while not item_being_tested.isnumeric():
            print(Error_Message1)
            time.sleep(.3)
            print("Please try again:")
            item_being_tested = input()
        int_value = int(item_being_tested)
    # 返回验证后的有效字符串
    return item_being_tested

修改后的调用代码

dates = []
month = input("month:")
# 将变量赋值为函数返回的有效输入
month = check_if_valid_number(month, "Month must be between 1-12 and not include letters", 12, 1)
day = input("day:")
day = check_if_valid_number(day, "Day must be between 1-31 and not include letters", 31, 1)
year = input("year:")
# 年份范围用float('inf')表示无限制
year = check_if_valid_number(year, "Year cannot include letters", float('inf'), float('-inf'))
dates.append("{}/{}/{}".format(month,day,year))

这样修改后,month、day、year变量会被更新为验证后的有效输入,列表存储的就是正确的内容了。

内容的提问来源于stack exchange,提问作者user19644078

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 13:03:21