简体   繁体   English

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

[英]Creating Temporary lists for Each set of user inputs

I am trying to create temporary lists to store individual weekly data for each store(I ask the user what the name of your store is and then ask about their daily sales for the week, which I then want to append into a separate list, but I don't know how to do it for multiple user inputs, which all have different sets of daily data for the week), I have created a permanent list to store all store's data in one list.我正在尝试创建临时列表来存储每个商店的个人每周数据(我询问用户您商店的名称是什么,然后询问他们本周的每日销售额,然后我想将其 append 放入单独的列表中,但是我不知道如何针对多个用户输入执行此操作,这些输入都有不同的一周每日数据集),我创建了一个永久列表以将所有商店的数据存储在一个列表中。 I am trying to create temporary lists for each store's data so that I can make a 2D list as a requirement in my project.我正在尝试为每个商店的数据创建临时列表,以便我可以在我的项目中制作一个 2D 列表作为要求。 I hope you understand.我希望你明白。

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

This is one way to do it.这是一种方法。 I put some inline comments to help explain what I did.我放了一些内联评论来帮助解释我做了什么。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM