Lead a life problem | Function that calculates the profit with the earning and cost of expenses ; To store the total Profit ; Loop for n number of days ; If last day , no need to buy food ; Else buy food to gain energy for next day ; Update earning per day ; Update profit with daily spent ; Print the profit ; Driver Code ; Given days ; Given earnings ; Given cost ; Given energy e ; Function call
def calculateProfit ( n , earnings , cost , e ) : NEW_LINE INDENT profit = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT earning_per_day = 0 ; NEW_LINE daily_spent_food = 0 ; NEW_LINE if ( i == ( n - 1 ) ) : NEW_LINE INDENT earning_per_day = earnings [ i ] * e ; NEW_LINE profit = profit + earning_per_day ; NEW_LINE break ; NEW_LINE DEDENT if ( cost [ i ] < earnings [ i ] ) : NEW_LINE INDENT earning_per_day = earnings [ i ] * e ; NEW_LINE daily_spent_food = cost [ i ] * e ; NEW_LINE profit = ( profit + earning_per_day - daily_spent_food ) ; NEW_LINE DEDENT DEDENT print ( profit ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE earnings = [ 1 , 8 , 6 , 7 ] ; NEW_LINE cost = [ 1 , 3 , 4 , 1 ] ; NEW_LINE e = 5 ; NEW_LINE calculateProfit ( n , earnings , cost , e ) ; NEW_LINE DEDENT