Python 如何设置计数事件?

Python 如何设置计数事件?,python,Python,我正在尝试设置一个计数事件,它将计算数组中有多少人,然后我想在计算中使用该数字来计算从门票中获得的总收入,但我不确定我该怎么做 每个customer\u ID都以一个c开头,我想用它作为搜索词 def read_info(): customer_id_list = [] ticket_id_list = [] number_of_tickets_list = [] buy_method_list = [] ticket_price = 0 tota

我正在尝试设置一个计数事件,它将计算数组中有多少人,然后我想在计算中使用该数字来计算从门票中获得的总收入,但我不确定我该怎么做

每个
customer\u 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)

既然我不能就你的问题发表评论,让我猜猜你想要什么

import csv


TICKET_PRICES = dict(F1=10, F2=10, F3=10, T1=5, T2=5, T3=5, W1=5, W2=5, W3=5)
DATA_COLUMNS = ('customer', 'ticket_type', 'ticket_count', 'ticket_total',
                'method')


def main():
    """Process ticket sale information from a database file."""
    data = read_data('data.txt')
    columns = pivot(data, DATA_COLUMNS)
    find_number_of_people(columns['customer'])


def read_data(path):
    """Get all ticket data from a file and start processing the information."""
    data = []
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for customer, ticket_type, count, method in reader:
            ticket_price = TICKET_PRICES[ticket_type]
            ticket_count = int(count)
            ticket_total = ticket_price * ticket_count
            row = customer, ticket_type, ticket_count, ticket_total, method
            data.append(row)
    return data


def pivot(data, column_names):
    """Convert the data table into a collection of related columns."""
    columns = {name: [] for name in column_names}
    for row in data:
        for key, value in zip(column_names, row):
            columns[key].append(value)
    return columns


def find_number_of_people(customers):
    """What am I supposed to do here? The current goal is not clear."""
    pass


if __name__ == '__main__':
    main()
如果要获取长度(即数组中的元素数),请尝试:

所以我看到你已经计算了每个顾客的总收入,只要把他们加起来,你就会得到所有门票的总收入。要执行此操作,请尝试:

grand_total = 0
for money in total_for_customer_list:
     grand_total = grand_total + money

如果我误解了你的意思,请在我的回答下与我讨论,以便我可以回答。

因为我不能在你的问题下发表评论,让我猜你想要什么

import csv


TICKET_PRICES = dict(F1=10, F2=10, F3=10, T1=5, T2=5, T3=5, W1=5, W2=5, W3=5)
DATA_COLUMNS = ('customer', 'ticket_type', 'ticket_count', 'ticket_total',
                'method')


def main():
    """Process ticket sale information from a database file."""
    data = read_data('data.txt')
    columns = pivot(data, DATA_COLUMNS)
    find_number_of_people(columns['customer'])


def read_data(path):
    """Get all ticket data from a file and start processing the information."""
    data = []
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for customer, ticket_type, count, method in reader:
            ticket_price = TICKET_PRICES[ticket_type]
            ticket_count = int(count)
            ticket_total = ticket_price * ticket_count
            row = customer, ticket_type, ticket_count, ticket_total, method
            data.append(row)
    return data


def pivot(data, column_names):
    """Convert the data table into a collection of related columns."""
    columns = {name: [] for name in column_names}
    for row in data:
        for key, value in zip(column_names, row):
            columns[key].append(value)
    return columns


def find_number_of_people(customers):
    """What am I supposed to do here? The current goal is not clear."""
    pass


if __name__ == '__main__':
    main()
如果要获取长度(即数组中的元素数),请尝试:

所以我看到你已经计算了每个顾客的总收入,只要把他们加起来,你就会得到所有门票的总收入。要执行此操作,请尝试:

grand_total = 0
for money in total_for_customer_list:
     grand_total = grand_total + money

如果我理解错了,请在我的答案下与我讨论,以便我可以回答。

以下是代码的重构版本。你能在问题中解释一下你想做什么吗?也许您可以完成下面显示的
main
函数,并假设存在多个函数,它们可以实现您想要的功能

import csv


TICKET_PRICES = dict(F1=10, F2=10, F3=10, T1=5, T2=5, T3=5, W1=5, W2=5, W3=5)
DATA_COLUMNS = ('customer', 'ticket_type', 'ticket_count', 'ticket_total',
                'method')


def main():
    """Process ticket sale information from a database file."""
    data = read_data('data.txt')
    columns = pivot(data, DATA_COLUMNS)
    find_number_of_people(columns['customer'])


def read_data(path):
    """Get all ticket data from a file and start processing the information."""
    data = []
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for customer, ticket_type, count, method in reader:
            ticket_price = TICKET_PRICES[ticket_type]
            ticket_count = int(count)
            ticket_total = ticket_price * ticket_count
            row = customer, ticket_type, ticket_count, ticket_total, method
            data.append(row)
    return data


def pivot(data, column_names):
    """Convert the data table into a collection of related columns."""
    columns = {name: [] for name in column_names}
    for row in data:
        for key, value in zip(column_names, row):
            columns[key].append(value)
    return columns


def find_number_of_people(customers):
    """What am I supposed to do here? The current goal is not clear."""
    pass


if __name__ == '__main__':
    main()

下面是代码的重构版本。你能在问题中解释一下你想做什么吗?也许您可以完成下面显示的
main
函数,并假设存在多个函数,它们可以实现您想要的功能

import csv


TICKET_PRICES = dict(F1=10, F2=10, F3=10, T1=5, T2=5, T3=5, W1=5, W2=5, W3=5)
DATA_COLUMNS = ('customer', 'ticket_type', 'ticket_count', 'ticket_total',
                'method')


def main():
    """Process ticket sale information from a database file."""
    data = read_data('data.txt')
    columns = pivot(data, DATA_COLUMNS)
    find_number_of_people(columns['customer'])


def read_data(path):
    """Get all ticket data from a file and start processing the information."""
    data = []
    with open(path, newline='') as file:
        reader = csv.reader(file)
        for customer, ticket_type, count, method in reader:
            ticket_price = TICKET_PRICES[ticket_type]
            ticket_count = int(count)
            ticket_total = ticket_price * ticket_count
            row = customer, ticket_type, ticket_count, ticket_total, method
            data.append(row)
    return data


def pivot(data, column_names):
    """Convert the data table into a collection of related columns."""
    columns = {name: [] for name in column_names}
    for row in data:
        for key, value in zip(column_names, row):
            columns[key].append(value)
    return columns


def find_number_of_people(customers):
    """What am I supposed to do here? The current goal is not clear."""
    pass


if __name__ == '__main__':
    main()

你有什么代码可以分享吗?展示你尝试过什么?我尝试过几件事情,但都没有成功,到目前为止,我必须说我不知道失败后从何处重新开始。我正在寻找一个计数事件的示例,它从数组中读取信息,我可以将其用作参考,并大致了解从何处开始。提供了一个新的答案,但是,一旦你提供了关于你想要完成什么的更多信息,它就会得到改进。你有什么代码可以分享吗?展示你尝试过什么?我尝试过几件事情,但都没有成功,到目前为止,我必须说我不知道失败后从何处重新开始。我正在寻找一个计数事件的示例,它从数组中读取信息,我可以将其用作参考,并大致了解从何处开始。提供了一个新的答案,但是,一旦你提供了关于你正在努力实现的目标的更多信息,它就会得到改进。