我正在嘗試創建臨時串列來存盤每個商店的個人每周資料(我詢問用戶您的商店的名稱是什么,然后詢問他們本周的每日銷售額,然后我想將其附加到單獨的串列中,但是我不知道如何處理多個用戶輸入,它們都有不同的一周每日資料集),我創建了一個永久串列,將所有商店的資料存盤在一個串列中。我正在嘗試為每個商店的資料創建臨時串列,以便我可以制作 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()
uj5u.com熱心網友回復:
這是一種方法。我放了一些行內評論來幫助解釋我做了什么。
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/462028.html
標籤:Python python-3.x 列表 循环
上一篇:在回圈中更改函式的名稱
