Bisection search not working in my python code - python

I am trying to find best savings rate per month using bisection search with given semi annual raise and rate of return on investment. The code runs on infinite loop.
# cost of the house
total_cost = 1000000
portion_down_payment = 0.25 * total_cost
current_savings = 0
annual_salary = int(input("enter your starting salary: "))
starting_salary = annual_salary / 12
semi_annual_raise = 0.07
# annual rate of return on investment is 4%
inv_rate = 0.04 / 12
months = 0
low = 0
high = 10000
number_steps = 0
# finding current savings using Bisection search
while abs((current_savings*36) - portion_down_payment) >= 100:
number_steps += 1
starting_salary = annual_salary / 12
for months in range(1, 37):
current_savings += (starting_salary * inv_rate)
# semi annual raise of 7 %
if months % 6 == 0:
starting_salary += starting_salary * 0.07
if current_savings * 36 < portion_down_payment:
low = current_savings
else:
high = current_savings
current_savings = (high + low) / 2
print("number of steps taken by bisection search is :", number_steps)
# printing savings rate
print(current_savings / starting_salary)

Related

Attribute change with variable number of time steps

I would like to simulate individual changes in growth and mortality for a variable number of days. My dataframe is formatted as follows...
import pandas as pd
data = {'unique_id': ['2', '4', '5', '13'],
'length': ['27.7', '30.2', '25.4', '29.1'],
'no_fish': ['3195', '1894', '8', '2774'],
'days_left': ['253', '253', '254', '256'],
'growth': ['0.3898', '0.3414', '0.4080', '0.3839']
}
df = pd.DataFrame(data)
print(df)
unique_id length no_fish days_left growth
0 2 27.7 3195 253 0.3898
1 4 30.2 1894 253 0.3414
2 5 25.4 8 254 0.4080
3 13 29.1 2774 256 0.3839
Ideally, I would like the initial length (i.e., length) to increase by the daily growth rate (i.e., growth) for each of the days remaining in the year (i.e., days_left).
df['final'] = df['length'] + (df['days_left'] * df['growth']
However, I would also like to update the number of fish that each individual represents (i.e., no_fish) on a daily basis using a size-specific equation. I'm fairly new to python so I initially thought to use a for-loop (I'm not sure if there is another, more efficient way). My code is as follows:
# keep track of run time - START
start_time = time.perf_counter()
df['z'] = 0.0
for indx in range(len(df)):
count = 1
while count <= int(df.days_to_forecast[indx]):
# (1) update individual length
df.lgth[indx] = df.lgth[indx] + df.linearGR[indx]
# (2) estimate daily size-specific mortality
if df.lgth[indx] > 50.0:
df.z[indx] = 0.01
else:
if df.lgth[indx] <= 50.0:
df.z[indx] = 0.052857-((0.03/35)*df.lgth[indx])
elif df.lgth[indx] < 15.0:
df.z[indx] = 0.728*math.exp(-0.1892*df.lgth[indx])
df['no_fish'].round(decimals = 0)
if df.no_fish[indx] < 1.0:
df.no_fish[indx] = 0.0
elif df.no_fish[indx] >= 1.0:
df.no_fish[indx] = df.no_fish[indx]*math.exp(-(df.z[indx]))
# (3) reduce no. of days left in forecast by 1
count = count + 1
# keep track of run time - END
total_elapsed_time = round(time.perf_counter() - start_time, 2)
print("Forecast iteration completed in {} seconds".format(total_elapsed_time))
The above code now works correctly, but it is still far to inefficient to run for 40,000 individuals each for 200+ days.
I would really appreciate any advice on how to modify the following code to make it pythonic.
Thanks
Another option that was suggested to me is to use the pd.dataframe.apply function. This dramatically reduced the overall the run time and could be useful to someone else in the future.
### === RUN SIMULATION === ###
start_time = time.perf_counter() # keep track of run time -- START
#-------------------------------------------------------------------------#
def function_to_apply( df ):
df['z_instantMort'] = ''
for indx in range(int(df['days_left'])):
# (1) update individual length
df['length'] = df['length'] + df['growth']
# (2) estimate daily size-specific mortality
if df['length'] > 50.0:
df['z_instantMort'] = 0.01
else:
if df['length'] <= 50.0:
df['z_instantMort'] = 0.052857-((0.03/35)*df['length'])
elif df['length'] < 15.0:
df['z_instantMort'] = 0.728*np.exp(-0.1892*df['length'])
whole_fish = round(df['no_fish'], 0)
if whole_fish < 1.0:
df['no_fish'] = 0.0
elif whole_fish >= 1.0:
df['no_fish'] = df['no_fish']*np.exp(-(df['z_instantMort']))
return df
#-------------------------------------------------------------------------#
sim_results = df.apply(function_to_apply, axis=1)
total_elapsed_time = round(time.perf_counter() - start_time, 2) # END
print("Forecast iteration completed in {} seconds".format(total_elapsed_time))
print(sim_results)
### ====================== ###
output being...
Forecast iteration completed in 0.05 seconds
unique_id length no_fish days_left growth z_instantMort
0 2.0 126.3194 148.729190 253.0 0.3898 0.01
1 4.0 116.5742 93.018465 253.0 0.3414 0.01
2 5.0 129.0320 0.000000 254.0 0.4080 0.01
3 13.0 127.3784 132.864757 256.0 0.3839 0.01
As I said in my comment, a preferable alternative to for loops in this setting is using vector operations. For instance, running your code:
import pandas as pd
import time
import math
import numpy as np
data = {'unique_id': [2, 4, 5, 13],
'length': [27.7, 30.2, 25.4, 29.1],
'no_fish': [3195, 1894, 8, 2774],
'days_left': [253, 253, 254, 256],
'growth': [0.3898, 0.3414, 0.4080, 0.3839]
}
df = pd.DataFrame(data)
print(df)
# keep track of run time - START
start_time = time.perf_counter()
df['z'] = 0.0
for indx in range(len(df)):
count = 1
while count <= int(df.days_left[indx]):
# (1) update individual length
df.length[indx] = df.length[indx] + df.growth[indx]
# (2) estimate daily size-specific mortality
if df.length[indx] > 50.0:
df.z[indx] = 0.01
else:
if df.length[indx] <= 50.0:
df.z[indx] = 0.052857-((0.03/35)*df.length[indx])
elif df.length[indx] < 15.0:
df.z[indx] = 0.728*math.exp(-0.1892*df.length[indx])
df['no_fish'].round(decimals = 0)
if df.no_fish[indx] < 1.0:
df.no_fish[indx] = 0.0
elif df.no_fish[indx] >= 1.0:
df.no_fish[indx] = df.no_fish[indx]*math.exp(-(df.z[indx]))
# (3) reduce no. of days left in forecast by 1
count = count + 1
# keep track of run time - END
total_elapsed_time = round(time.perf_counter() - start_time, 2)
print("Forecast iteration completed in {} seconds".format(total_elapsed_time))
print(df)
with output:
unique_id length no_fish days_left growth
0 2 27.7 3195 253 0.3898
1 4 30.2 1894 253 0.3414
2 5 25.4 8 254 0.4080
3 13 29.1 2774 256 0.3839
Forecast iteration completed in 31.75 seconds
unique_id length no_fish days_left growth z
0 2 126.3194 148.729190 253 0.3898 0.01
1 4 116.5742 93.018465 253 0.3414 0.01
2 5 129.0320 0.000000 254 0.4080 0.01
3 13 127.3784 132.864757 256 0.3839 0.01
Now with vector operations, you could do something like:
# keep track of run time - START
start_time = time.perf_counter()
df['z'] = 0.0
for day in range(1, df.days_left.max() + 1):
update = day <= df['days_left']
# (1) update individual length
df[update]['length'] = df[update]['length'] + df[update]['growth']
# (2) estimate daily size-specific mortality
df[update]['z'] = np.where( df[update]['length'] > 50.0, 0.01, 0.052857-( ( 0.03 / 35)*df[update]['length'] ) )
df[update]['z'] = np.where( df[update]['length'] < 15.0, 0.728 * np.exp(-0.1892*df[update]['length'] ), df[update]['z'] )
df[update]['no_fish'].round(decimals = 0)
df[update]['no_fish'] = np.where(df[update]['no_fish'] < 1.0, 0.0, df[update]['no_fish'] * np.exp(-(df[update]['z'])))
# keep track of run time - END
total_elapsed_time = round(time.perf_counter() - start_time, 2)
print("Forecast iteration completed in {} seconds".format(total_elapsed_time))
print(df)
with output
Forecast iteration completed in 1.32 seconds
unique_id length no_fish days_left growth z
0 2 126.3194 148.729190 253 0.3898 0.0
1 4 116.5742 93.018465 253 0.3414 0.0
2 5 129.0320 0.000000 254 0.4080 0.0
3 13 127.3784 132.864757 256 0.3839 0.0

first python program, having trouble looping [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
Basically i want my output from previous value to be new input.
print ("Welcome to Derivative Risk Calculator")
while True:
try:
deposit2 = float(input("\nEnter initial deposit: "))
RSK = float(input("Risk: "))
rate = float(input("Rate: "))
md = float(input("monthly deposits: "))
if deposit2<1:
print ("Not a valid amount, please try again.")
else:
break
except ValueError:
print ("You have not entered a number. Please enter a valid number")
for month in range(24):
amount = (((deposit2 * RSK) * rate) * 30 + md + deposit2)
new_amount = amount
print("%4d%21.2f" % (month, new_amount ))
Current output:
Enter initial deposit: 55000
Risk: 0.38
Rate: 0.025
monthly deposits: 10000
0 80675.00
1 80675.00
2 80675.00
3 80675.00
4 80675.00
5 80675.00
6 80675.00
7 80675.00
8 80675.00
9 80675.00
10 80675.00
11 80675.00
12 80675.00
13 80675.00
14 80675.00
15 80675.00
16 80675.00
17 80675.00
18 80675.00
19 80675.00
20 80675.00
21 80675.00
22 80675.00
23 80675.00
Desired output:
Enter initial deposit: 55000
Risk: 0.38
Rate: 0.025
monthly deposits: 10000
0 80675.00
1 113667.00
2 156063.00
3 210540.00
I basically want each output value of input for next process. For example 80675 becomes the new deposit2 value.
Please help me in this
You simply need to reassign deposit2 to amount for every iteration of the loop. Changing the value of variables is a very common thing to do, and there's nothing wrong with doing it. Also, the variable new_amount is not needed, you can simply just change deposit2 to amount once it is calculated. Here's your code, but fixed:
print ("Welcome to Derivative Risk Calculator")
while True:
try:
deposit2 = float(input("\nEnter initial deposit: "))
RSK = float(input("Risk: "))
rate = float(input("Rate: "))
md = float(input("monthly deposits: "))
if deposit2<1:
print ("Not a valid amount, please try again.")
else:
break
except ValueError:
print ("You have not entered a number. Please enter a valid number")
for month in range(24):
amount = (((deposit2 * RSK) * rate) * 30 + md + deposit2)
deposit2 = amount
print("%4d%21.2f" % (month, deposit2 ))
Set a new "amount" variable equal to the user input (deposit2) to
start and then just update it in the loop.
print ("Welcome to Derivative Risk Calculator")
while True:
try:
deposit2 = float(input("\nEnter initial deposit: "))
RSK = float(input("Risk: "))
rate = float(input("Rate: "))
md = float(input("monthly deposits: "))
if deposit2<1:
print ("Not a valid amount, please try again.")
else:
break
except ValueError:
print ("You have not entered a number. Please enter a valid number")
amount = deposit2
for month in range(24):
amount = amount * RSK * rate * 30 + md + amount
print("%4d%21.2f" % (month, amount))

How do I get a the output of a range() function to interact with my bisection search method?

The following code is supposed to calcualte what the savings rate would be to pay off my down payment in exactly 36 months given certain factors like (salary raises and annual return rates. I am having trouble getting the output of the range function to interact with my bisection search method.
# Cost
total_cost = 1000000
portion_down_payment = .25 * total_cost
# Salary
annual_salary = 150000
semi_annual_raise = .07
annual_return_rate = .04
current_savings = 0
month = 0
# Bisection
epsilon = 100
num_guesses = 0
low = 0
high = 1
saving_rate = (high + low) / 2.0
portion_saved_monthly = annual_salary / 12 * saving_rate
# range()function
for x in range(36):
current_savings += (current_savings * annual_return_rate / 12) + portion_saved_monthly
month += 1
if month % 6 == 0:
annual_salary += annual_salary * semi_annual_raise
portion_saved_monthly = annual_salary / 12 * saving_rate
while abs(current_savings - portion_down_payment) >= epsilon:
if current_savings < portion_down_payment:
low = saving_rate
else:
high = saving_rate
saving_rate = (high + low) / 2.0
num_guesses += 1
print('Best savings rate: ', saving_rate)
print('Steps in bisection search: ', num_guesses)
I started with the while loop and nested the for loop inside. I also moved some variables inside the while loop which helped me get the right answer.
'''
# Cost of House
total_cost = 1000000
portion_down_payment = .25 * total_cost
# Salary
annual_salary_input = float(input('Enter the starting salary: '))
current_savings = 0
# Bisection
epsilon = 100
num_guesses = 0
low = 0
high = 1
saving_rate = (high + low) / 2.0
portion_saved_monthly = annual_salary_input / 12 * saving_rate
while abs(current_savings - portion_down_payment) >= epsilon:
annual_salary = annual_salary_input
semi_annual_raise = .07
annual_return_rate = .04
current_savings = 0
month = 0
portion_saved_monthly = annual_salary / 12 * saving_rate
saving_rate = (high + low) / 2.0
# Loop
for x in range(36):
current_savings += (current_savings * annual_return_rate / 12) + portion_saved_monthly
month += 1
if month % 6 == 0:
annual_salary += annual_salary * semi_annual_raise
portion_saved_monthly = annual_salary / 12 * saving_rate
if current_savings < portion_down_payment:
low = saving_rate
else:
high = saving_rate
saving_rate = (high + low) / 2.0
num_guesses += 1
if num_guesses > 1000:
break
if saving_rate < 1.0:
print('Best savings rate: ', "%.4f" % saving_rate)
print('Steps in bisection search: ', num_guesses)
else:
print('It is not possible to pay the down payment in three years.')
'''

How to sum specific integer values of Python List

I'm trying to sum integer values from the list using sum function. Unfortunately, it is adding all the values of the list but not those which I need from the user.
Here is my code:
tourist_attractions = []
distance = []
entry_cost = []
for i in range(3):
tourist_attractions.append (input("Enter Tourist place: "))
tourist_distance =(int(input("Enter distance: ")))
if tourist_distance > 50:
print("Invalid Entry")
continue
if tourist_distance <= 50:
distance.append(tourist_distance)
cost = (float(input("Enter cost: ")))
if cost > 100:
print("cost must be between 1-100")
continue
if cost > 0 or cost <= 100:
entry_cost.append(cost)
print()
for line in tourist_attractions:
print("Place:", line)
for line in distance:
print("Distance:", line)
for line in entry_cost:
print("Cost:", line)
print()
number_of_places_to_visit = int(input("Total number of places to visit: "))
x = 1
while x <= number_of_places_to_visit:
select_tourist_place = input("select tourist place, 0-3: ")
x = x + 1
if select_tourist_place == "0":
print(tourist_attractions[0], distance[0], entry_cost[0])
elif select_tourist_place == "1":
print(tourist_attractions[1], distance[1], entry_cost[1])
elif select_tourist_place == "2":
print(tourist_attractions[2], distance[2], entry_cost[2])
elif select_tourist_place == "3":
print(tourist_attractions[3], distance[3], entry_cost[3])
elif select_tourist_place == "4":
print(tourist_attractions[4], distance[4], entry_cost[4])
print("total cost: " , sum(entry_cost))
Result I am getting:
Enter Tourist place: London
Enter distance: 25
Enter cost: 15
Enter Tourist place: Manchester
Enter distance: 30
Enter cost: 15
Enter Tourist place: Scotland
Enter distance: 50
Enter cost: 20
Place: London
Place: Manchester
Place: Scotland
Distance: 25
Distance: 30
Distance: 50
Cost: 15.0
Cost: 15.0
Cost: 20.0
Total number of places to visit: 2
select tourist place, 0-3: 0
London 25 15.0
select tourist place, 0-5: 1
Manchester 30 15.0
total cost: 50.0
>>>
I can understand, at the moment it is summing up all the appended list of entry_cost and giving me the total of 50 which should be 15 from London and 15 from Manchester. Any help?
print("total cost: " , sum(entry_cost))
definitely states your are iterating over ALL entry costs. You would want to store the selected indices and sum over the entries of those indices.

How to optimize (= shorten) my Python code?

My use-case is as follows:
I want to make an electric bill generator with different units having different prices. In below program, if the user enters units between 1-150 then for making price value it will do multiplication with 2.50 and make a price. If the user Units are between the 151-300 then it will multiply the first 150 units with 2.50 and left units will multiply with the 3.60 means to say If user entered 155 then for first 150 150 * 2.50 and for left units 5 it will do multiplication 5 * 3.60. like the above I said I'm making like this four variations given Below:-
if units lies between 1-150 then it will multiplies with the 2.50 and calculates the price.
if units lies between 151-300 then it will multiplies first 150 units with the price 2.50 and left units below the 300 will multiply with the 3.60 and calculates the price.
if units lies between 301-450 then it will multiplies first 150 units with the price 2.50 and other 150 units will multiplies with the 3.60 and left units are multiplies with the 4.75 and calculates the price.
if units lies between 451-600 then it will multiplies first 150 units with the price 2.50 and other 150 units will multiplies with the 3.60 and other 150 units are multiplies with the 4.75 and left units are multiplies with the 5 and calculates the price.
if units lies above 600 then it will multiplies first 150 units with the price 2.50 and other 150 units will multiplies with the 3.60 and other 150 units are multiplies with the 4.75 and next 150 units are multiplies with the 5 and left units are multiplies with the 6 and calculates the price.
Here is my code which is seek to optimize, i.e. shorten:
units = int(input("Enter the units"))
if(1<=units and 150>=units):
firstSum = units * 2.50
print("First sum:-", firstSum)
if(151<=units and 300>=units):
firstSum = 150 * 2.50
subUnits = units - 150
secondSum = firstSum + (subUnits * 3.60)
print(secondSum)
if(301<=units and 450>=units):
firstSum = 150 * 2.50
subUnits1 = units - 150
firstSum += 150 * 3.60
subUnits = subUnits1 - 150
secondSum = firstSum + subUnits * 4.75
print(secondSum)
if(451<=units and 600>= units):
firstSum = 150 * 2.50
subUnits1 = units - 150
firstSum += 150 * 3.60
subUnits1 -= 150
firstSum += subUnits1 * 4.75
subUnits = subUnits1 - 150
secondSum = firstSum + subUnits * 5
print(secondSum)
if(601<=units):
firstSum = 150 * 2.50
subUnits1 = units - 150
firstSum += 150 * 3.60
subUnits1 -= 150
firstSum += subUnits1 * 4.75
subUnits2 = subUnits1 - 150
firstSum += 150 * 5
subUnits = subUnits2 - 150
secondSum = firstSum + subUnits * 6
print(secondSum)
Can any one help me to make my program in short way.
Thank you for your precious time
If I understand well your problem, I don't think your code is doing the right thing for the two last cases. It looks like a wrong copy/paste of the previous cases ;)
I think that for the two last cases you should have :
if(451<=units and 600>= units):
firstSum = 150 * 2.50
subUnits1 = units - 150
firstSum += 150 * 3.60
subUnits1 -= 150
firstSum += 150 * 4.75
subUnits = subUnits1 - 150
secondSum = firstSum + subUnits * 5
print(secondSum)
if(601<=units):
firstSum = 150 * 2.50
subUnits1 = units - 150
firstSum += 150 * 3.60
subUnits1 -= 150
firstSum += 150 * 4.75
subUnits2 = subUnits1 - 150
firstSum += 150 * 5
subUnits = subUnits2 - 150
secondSum = firstSum + subUnits * 6
print(secondSum)
To answer your question, if I understood well, you can do something like :
units = int(input("Enter the units : "))
factor_list = [2.5, 3.6, 4.75, 5]
last_factor = 6
upper_bound = 600
step = 150
SUM = 0
if (units > upper_bound):
SUM += (units-upper_bound)*last_factor
units = upper_bound
nb150 = units/step
for i in range(0,int(nb150)):
SUM += step*factor_list[i]
if(int(nb150) < len(factor_list)):
SUM += (units-int(nb150)*step)*factor_list[int(nb150)]
print(SUM)
This solution simply avoid the multiple if statements by computing the euclidean division of units. That way you can easily change the coefficients or add others without needing to write other cases.
The first if statement takes care of all the units that are greater than the upper bound. It basically multiplies all the units above 600 with 6 and remove them from the units to be handled.
By the line nb150 = units/step and taking the integer part, I obtain the number of groups of 150 units. Then I can multiply them by their corresponding coefficient in the for loop.
Finally, if the number of units is lower than 600 but not a multiple of 150, the code needs to take care of the rest. So it removes the groups of 150 : (units-int(nb150)*step), then multiplies the rest with the corresponding factor factor_list[int(nb150)].
If you need further explanation, feel free to ask !
Talking about optimization, there's quite less you can do in order to optimize the code...
You can definitely optimize it by using if-elif-else conditional statements instead of using just if :
if(1<=units and 150>=units):
...
elif(151<=units and 300>=units):
...
:
:
else:
...
When you are doing so, you are making sure that the condition checking doesn't happen after the right condition is reached. Thereby, reducing the number of comparisons done and optimizing the program.
Special reason why you need it shortened? Anyway you might start by refactoring repeating code blocks into methods. For example
firstSum = 150 * 2.50
subUnits1 = units - 150
firstSum += 150 * 3.60
subUnits = subUnits1 - 150
Happens three times.
Also is there a special reasons for all the if statements instead of elif? Not that it would make the code shorter.
If you don't mind the unreadability here is your one-liner:
print(units * 2.5 + max(0, units - 150) * 1.1 + max(0, units - 300) * 1.15 + max(0, units - 450) * 0.25 + max(0, units - 600))
Also your example code is buggy on line 23 (firstSum += subUnits1 * 4.75), it should multiply with another 150 there.
Given your code (regardless your description); you can shorten your code by doing the math, e.g.:
def bill_generator(units):
firstSum = min(units, 150) * 2.5
if units <= 300:
secondSum = firstSum + units * 3.60 - 540.0
elif units <= 450:
firstSum += 540
secondSum = firstSum + units * 4.75 - 1425.0
elif units <= 600:
firstSum = 540 + units * 4.75 - 1425.0
secondSum = firstSum + units * 5.0 - 2250.0
else:
firstSum = 150.0 * 11.1 + units * 4.75 - 1425.0
secondSum = firstSum + units * 6.0 - 3600.0
print("FirstSum:-{}".format(firstSum))
if units > 150:
print(secondSum)
if __name__ == '__main__':
inp_units = int(input("Enter the units: "))
while inp_units < 1:
print("invalid input, units must be greater than zero")
inp_units = int(input("Enter the units"))
Tested the border cases:
def bill_generator(units):
firstSum = min(units, 150) * 2.5
if units <= 300:
secondSum = firstSum + units * 3.60 - 540.0
elif units <= 450:
firstSum += 540.0
secondSum = firstSum + units * 4.75 - 1425.0
elif units <= 600:
firstSum += 540.0 + units * 4.75 - 1425.0
secondSum = firstSum + units * 5.0 - 2250.0
else:
firstSum = 1665.0 + units * 4.75 - 1425.0
secondSum = firstSum + units * 6.0 - 3600.0
print("FirstSum:-{}".format(firstSum))
if units > 150:
print(secondSum)
if __name__ == '__main__':
for ii in [1, 150, 151, 300, 301, 450, 451, 600, 601, 1200]:
print('Testing for unit input "{}"'.format(ii))
bill_generator(ii)
'''
Testing for unit input "1"
FirstSum:-2.5
Testing for unit input "150"
FirstSum:-375.0
Testing for unit input "151"
FirstSum:-375.0
378.6
Testing for unit input "300"
FirstSum:-375.0
915.0
Testing for unit input "301"
FirstSum:-915.0
919.75
Testing for unit input "450"
FirstSum:-915.0
1627.5
Testing for unit input "451"
FirstSum:-1632.25
1637.25
Testing for unit input "600"
FirstSum:-2340.0
3090.0
Testing for unit input "601"
FirstSum:-3094.75
3100.75
Testing for unit input "1200"
FirstSum:-5940.0
9540.0
'''
You are adding your values to variables again and again in newlines rather than binding them up in a single line. Your shorted code can be as:
units = int(input("Enter the units: "))
if 1<=units and 150>=units:
print("First sum:-", units * 2.50)
elif 300>=units:
print((150 * 2.50) + ((units - 150) * 3.60))
elif 450>=units:
print(((150 * 2.50)+150 * 3.60) + ((units - 300) * 4.75))
elif 600>= units:
print((((150 * 2.50) + 150 * 3.60) + (units - 300) * 4.75) + ((units - 300) - 150 ) * 5)
else:
print(((((150 * 2.50) +150 * 3.60)+(units - 300) * 4.75)+150 * 5) + (((units - 300) - 150) - 150) * 6)

Categories