簡體   English   中英

為什么我的列表只收集第一個輸入?

[英]Why is my list only collecting the first input?

我試圖列出我過去所做的所有購買並以列表格式查看它們,每當我嘗試將無效語句放入列表時,它應該拒絕該語句並提示輸入新的輸入,而是它只是抓住第一個輸入並存儲它。

如果我輸入 w、x、y、z 但必須重新輸入 z 因為它無效,(m 將更改值)output 仍將 w、x、y、z 不變,我如何獲取要打印的列表 w ,x,y,m?

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

Function 檢查是否為有效數字

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", inf, inf)
dates.append("{}/{}/{}".format(month,day,year))

在這個例子中,即使我先將 33 作為輸入,然后將其更改為 5,列表將存儲 33

check_if_valid_number()需要返回更新后的值,然后您可以將其分配回變量。

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()
    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()
    return int(item_being_tested)

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

不需要這條線:

    if int_number_value > Maximum or int_number_value < Minimum:

因為它被while循環的條件所復制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM