BloodyRequiem
BloodyRequiem

Reputation: 25

Values related to a max value in a for loop python

I'm now able to print the maximum value for profit in my for loop but I also need to print values related to it for the number of passengers and ticket value that allowed for it to be the max value. I haven't been able to figure out how to print those values.

Loop = "y"
FixedCost = 2500

while Loop == "y":

Input

    MP = int(input("1 of 3 - Enter the minimum number of passengers: "))
    print ("The minimum number of passengers is set to ", MP)

    MaxP = int(input("2 of 3 - Enter the maximum number of passengers: "))
    print ("The minimum number of passengers is set to ", MaxP)

    TP = float(input("3 of 3 - Enter the ticket price: "))

Header for the following print in the for loop

    print ("Number of", end="    ")
    print ("Ticket")
    print ("Passengers", end="   ")
    print ("Price", end="             ")
    print ("Gross", end="              ")
    print ("Fixed Cost", end="          ")
    print ("Profit")

For Loop

    profits = [] 

    for NP in range(MP, MaxP+1, 10):

        CostofTicket = TP - (((NP - MP)/10)*.5)
        Gross = NP * CostofTicket
        Profit = (NP * CostofTicket) - FixedCost

        profits.append(Profit)

        print (NP, end="          ")
        print ("$", format(CostofTicket, "3,.2f"), end="          ")
        print ("$", format(Gross, "3,.2f"), end="          ")
        print ("$", format(FixedCost, "3,.2f"), end="          ")
        print ("$", format(Profit, "3,.2f"))

Print for max value and related values

    greatest_profit = max(profits)
    print ("Maximum Profit: ", end="")
    print (format(greatest_profit, "3,.2f"))

    print ("Maximum Profit Ticket Price: ")

    print ("Maximum Profit Number of Passengers: ")

    Loop = str(input("Run this contract again (Y or N): "))

Upvotes: 0

Views: 116

Answers (1)

tdelaney
tdelaney

Reputation: 77337

max (and sorting in general) works with collections such as tuple and list as well as integers. Just build a tuple with Profit as the first item and whatever else you want associated with it, and use that instead of Profit itself.

I wasn't sure which variables from the loop you wanted to keep, so I just winged it...

profits = [] 

for NP in range(MP, MaxP+1, 10):

    CostofTicket = TP - (((NP - MP)/10)*.5)
    Gross = NP * CostofTicket
    Profit = (NP * CostofTicket) - FixedCost

    profits.append((Profit, TP, NP, MP))

    print (NP, end="          ")
    print ("$", format(CostofTicket, "3,.2f"), end="          ")
    print ("$", format(Gross, "3,.2f"), end="          ")
    print ("$", format(FixedCost, "3,.2f"), end="          ")
    print ("$", format(Profit, "3,.2f"))

# find max profits tuple and unpack its values
Profit, TP, NP, MP = max(profits)
# etc...

Upvotes: 1

Related Questions