def sales_report(txt_doc):
# Define a function that takes text file order-by-type as an argument
    f = open(txt_doc)
    # Assign text file to a variable
    melon_tallies = {"Musk":0, "Hybrid":0, "Watermelon":0, "Winter": 0}
    # Start tallies for each type of melon

    for l in f:
    # For each line in the file
        data = l.split("|")
        # Split data by "|"
        melon_type = data[1]
        # Define melon type as the element at index 1
        melon_count = int(data[2])
        # Define melon count as the element at index 2
        melon_tallies[melon_type] += melon_count
        # Tally each melon type into melon tallies and add total sum to melon count

    melon_prices = { "Musk": 1.15, "Hybrid": 1.30, "Watermelon": 1.75, "Winter": 4.00 }
    # Define melon prices

    for melon_type in melon_tallies:
    # For each melon type in the melon tallies
        price = melon_prices[melon_type]
        # Price is equal to each melon price of each melon type
        revenue = price * melon_tallies[melon_type]
        # Revenue is the price per melon times the tally of each melon type
        # print("We sold %d %s melons at %0.2f each for a total of %0.2f" % (melon_tallies[melon_type], melon_type, price, revenue))
        print("We sold {} {} melons at {:.2f} each for a total of {:.2f}".format(melon_tallies[melon_type], melon_type, price, revenue))

sales_report("orders-by-type.txt")
# Call function that passes "orders-by-type.txt"

def order_report(txt_file):
# Define a function that takes text file order-with-sales as an argument
    f = open(txt_file)
    # Assign text file to a variable
    sales = [0, 0]
    # Set sales variable as empty list
    for line in f:
    # For each line in the file
        d = line.split("|")
        # Split the data at the "|"
        if d[1] == "0":
        # If the value at index 1 is 0
            sales[0] += float(d[3])
            # Add the value at index 3 to the total sold by internet
        else:
            sales[1] += float(d[3])
            # Else, add the value at index 3 to the total sold by salespeople
    print("Salespeople generated ${:.2f} in revenue.".format(sales[1]))
    # Print what was generated by salespeople (sales[1])
    print("Internet sales generated ${:.2f} in revenue.".format(sales[0]))
    # Print what was generated by internet (sales[0])
    if sales[1] > sales[0]:
        print("Guess there's some value to those salespeople after all.")
        # If more was sold by salespeople than the internet, salespeople win.
    else:
        print("Time to fire the sales team! Online sales rule all!")
        # If more was sold on the internet than by salespeople, the internet wins.

order_report("orders-with-sales.txt")
# Call function that passes "order-with-sales.txt"

# Homework instructions:

# SALESPERSON_INDEX = 0
# INTERNET_INDEX = 1
# DORKY_LINE_LENGTH = 80

# print("*" * DORKY_LINE_LENGTH)
# f = open("orders-by-type.txt")
# melon_tallies = {"Musk":0, "Hybrid":0, "Watermelon":0, "Winter": 0}

# for l in f:
#     data = l.split("|")
#     melon_type = data[1]
#     melon_count = int(data[2])
#     melon_tallies[melon_type] += melon_count

# f.close()
# melon_prices = { "Musk": 1.15, "Hybrid": 1.30, "Watermelon": 1.75, "Winter": 4.00 }
# total_revenue = 0
# for melon_type in melon_tallies:
#     price = melon_prices[melon_type]
#     revenue = price * melon_tallies[melon_type]
#     total_revenue += revenue
#     # print("We sold %d %s melons at %0.2f each for a total of %0.2f" % (melon_tallies[melon_type], melon_type, price, revenue))
#     print("We sold {} {} melons at {:.2f} each for a total of {:.2f}".format(melon_tallies[melon_type], melon_type, price, revenue))
# print("******************************************")
# f = open("orders-with-sales.txt")
# sales = [0, 0]
# for line in f:
#     d = line.split("|")
#     if d[1] == "0":
#         sales[0] += float(d[3])
#     else:
#         sales[1] += float(d[3])
# print("Salespeople generated ${:.2f} in revenue.".format(sales[1]))
# print("Internet sales generated ${:.2f} in revenue.".format(sales[0]))
# if sales[1] > sales[0]:
#     print("Guess there's some value to those salespeople after all.")
# else:
#     print("Time to fire the sales team! Online sales rule all!")
# print("******************************************")
