为每组用户输入创建临时列表

阿维纳什·拉贾拉姆

我正在尝试创建临时列表来存储每个商店的个人每周数据(我询问用户您的商店名称是什么,然后询问他们本周的每日销售额,然后我想将其附加到单独的列表中,但是我不知道如何处理多个用户输入,它们都有不同的一周每日数据集),我创建了一个永久列表,将所有商店的数据存储在一个列表中。我正在尝试为每个商店的数据创建临时列表,以便我可以制作 2D 列表作为我的项目的要求。我希望你明白。

All_Store_Daily_income = []
days_of_the_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
is_last_weekday = None

def get_daily_store_data():
    Day_Counter = 0
    while Day_Counter < 7:
        try:       
            Daily_sales = float(input(f"What is the income for {days_of_the_week[Day_Counter]}? "))
            All_Store_Daily_income.append(Daily_sales)
            print(All_Store_Daily_income)
        
            Day_Counter = Day_Counter + 1
            global is_last_weekday
            is_last_weekday = Day_Counter == 7
        except ValueError:
            print("Please enter a integer or float, no letters allowed!")


def store_adder_programme():
    Store_name_list = []
    number_of_stores = 1
    Adding_stores = 'y'

while Adding_stores == 'y':

    store_name = input("What is the name of your registered store? ")
    Store_name_list.append(store_name)
    get_daily_store_data()
    print(Store_name_list)

    if (is_last_weekday == True) and (Adding_stores == 'y'):
        print('Would you like to add a new store?')
        Adding_stores = input("If YES(y), If NO(n): ")
        print(All_Store_Daily_income)                        
        number_of_stores = number_of_stores + 1

store_adder_programme() 
alexpdev

这是一种方法。我放了一些内联评论来帮助解释我做了什么。

All_Store_Daily_income = []  # this becomes 2D list

days_of_the_week = ["Monday", "Tuesday", "Wednesday", "Thursday", 
                    "Friday", "Saturday", "Sunday"]
is_last_weekday = None


def get_daily_store_data():
    seq = []  # create a list

    Day_Counter = 0
    while Day_Counter < 7:
        try:
            Daily_sales = float(input(f"What is the income for {days_of_the_week[Day_Counter]}? "))

            seq.append(Daily_sales)  # fill list with store data

            Day_Counter = Day_Counter + 1
            global is_last_weekday
            is_last_weekday = Day_Counter == 7
        except ValueError:
            print("Please enter a integer or float, no letters allowed!")

    return seq # return list


def store_adder_programme():
    Store_name_list = []
    number_of_stores = 1
    Adding_stores = 'y'

    while Adding_stores == 'y':
        store_name = input("What is the name of your registered store? ")
        Store_name_list.append(store_name)

        store_data = get_daily_store_data() # get list from function return value

        All_Store_Daily_income.append(store_data)  # append returned list to All_Store_Daily_income

        print(Store_name_list)

        if is_last_weekday and Adding_stores == 'y':
            print('Would you like to add a new store?')
            Adding_stores = input("If YES(y), If NO(n): ")
            print(All_Store_Daily_income)  # prints the 2D list
            number_of_stores = number_of_stores + 1

store_adder_programme()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章