我試圖設置一個計數事件,它將計算一個數組中有多少人,然後我想在計算中使用該數字來計算從這些票中賺取的總金額,但我不知道如何我應該這樣做。如何設置計數事件?
每個customer_ID
都以c
開頭,我想用它作爲搜索詞。
def read_info():
customer_id_list = []
ticket_id_list = []
number_of_tickets_list = []
buy_method_list = []
ticket_price = 0
total_for_customer_list = []
file = open ("Data.txt","r")
for line in file:
#Splits the data from the text file into it's corresponding variables.
customer_id,ticket_id,number_of_tickets,buy_method = line.split(",",4)
#Adds customer_id into the customer_id_list array
customer_id_list.append(customer_id)
# Adds ticket_id into the ticket_id_list array
ticket_id_list.append(ticket_id)
# Adds number_of_tickets into the number_of_tickets_list array
number_of_tickets_list.append(number_of_tickets)
#Adds the buy_method into the buy_method_list array
buy_method_list.append(buy_method)
#Using IF statements the program works out the price for each day which will be later used to calculate the total price for the customer and the total raised for charity.
if ticket_id in ['F1','F2','F3']:
ticket_price = 10
if ticket_id in ['W1','W2','W3','T1','T2','T3']:
ticket_price = 5
#converts the ticket_price from a string variable to a integer variable which allows this variable to be used in a calculation.
ticket_price = int(ticket_price)
#converts the number_of_tickets from a string variable to a integer variable which will be used in a calculation.
number_of_tickets = int(number_of_tickets)
#calculates the total price that will be paid for each customer by multyplying the ticket_Price which was worked out using the 'IF' statements by the number_of_tickets the customer has bought
total_for_customer = ticket_price * number_of_tickets
return customer_id_list,ticket_id_list,number_of_tickets_list,buy_method_list,ticket_price,total_for_customer
def find_number_of_people (customer_id_list):
#main program
customer_id_list,ticket_id_list,number_of_tickets_list,buy_method_list,ticket_price,total_for_customer = read_info()
find_number_of_people (customer_id_list)
你有任何可以分享的代碼嗎?顯示你試過的東西? –
我曾嘗試過幾件事,但沒有任何工作,到目前爲止,我必須說我不知道我失敗後從哪裏開始。 – adiwitko
我一直在尋找一個計數事件的例子,它從一個數組中讀取信息,我可以用它作爲參考,從中得到一個粗略的想法。 – adiwitko