How do I fix this code to get the proper output - python-3.x

This is the code:
amount_borrowed = float(input("Amount borrowed: $"))
interest_rate = float(input("Interest rate: "))
loan_length = int(input("Length of loan (months): "))
i = interest_rate/100
monthly_payment1 = (i / 12) * amount_borrowed #this is the first part of the payment formula
monthly_payment2 = monthly_payment1 / 1 - (1 + i / 12)**-loan_length
#second part of monthly payment formula
print("The monthly payment is ${:.2f}" .format(monthly_payment2))
I should get this output:
Amount borrowed: $100.00
Interest rate: 10.0
Length of loan (months): 12
The monthly payment is $8.79.
I keep on getting this:
Amount borrowed: $100
Interest rate: 10
Length of loan (months): 12
The monthly payment is $-0.07

Don't make much sense divide by 1. I think that you did forget a pair of parentheses in the denominator of the division. So, the following line
monthly_payment2 = monthly_payment1 / 1 - (1 + i / 12)**-loan_length
should be
monthly_payment2 = monthly_payment1 / (1 - (1 + i / 12)**-loan_length)
I tested here and get the expect value of $8.79

Related

Calculate probability of an event not by exclusion

I have some doubt with these kind of problems, example:
"If we asked 20,000 in a stadium to toss a coin 10 times, what it's the probability of at least one person getting 10 heads?"
I took this example from Practical Statistics for Data Scientist.
So, the probability of at least one person getting 10 heads it's calculated using: 1 - P(of nobody in the stadium getting 10 heads).
So we kind of doing an exclude procedure here, first I get the probability of the contrary event I am trying to measure, not the ACTUAL experiment I want to measure: at least one people getting 10 heads.
Why do we do it this way?
How can I calculate the probability of at least someone getting 10 heads but without passing through the probability of no one getting 10 heads?
As #Robert Dodier mentioned in the comments, the reason is that the calculations are simpler. I will use a stadium of 20 people instead of 20000 as an example:
Method 1:
Probability of not getting 10 heads for one individual
= 1 - probability of getting 10 heads
= 1 - 10!/(10!0!)*0.5^10*(1-0.5)^0
= 0.9990234375
Probability of at least one person in the stadium getting 10 heads
= 1 - P(of nobody in the stadium getting 10 heads)
= 1 - 0.9990234375**20 (because all coin tosses are independent)
= 0.019351109194852834
Method 2:
Probability of getting 10 heads for one individual
= 10!/(10!0!)*0.5^10*(1-0.5)^0
= 0.0009765625
Probability of exactly 1, 2, 3, etc. persons in the stadium getting 10 heads:
p1 = 20!/(1!19!)*0.0009765625^1*(1-0.0009765625)^(20-1) = 0.019172021325613825
p2 = 20!/(2!18!)*0.0009765625^2*(1-0.0009765625)^(20-2) = 0.00017803929872270904
p3 = 20!/(3!17!)*0.0009765625^3*(1-0.0009765625)^(20-3) = 1.0442187608370032e-06
p4 = 20!/(4!16!)*0.0009765625^4*(1-0.0009765625)^(20-4) = 4.338152232216289e-09
p5 = 20!/(5!15!)*0.0009765625^5*(1-0.0009765625)^(20-5) = 1.3569977656981548e-11
p6 = 20!/(6!14!)*0.0009765625^6*(1-0.0009765625)^(20-6) = 3.316221323798032e-14
p7 = 20!/(7!13!)*0.0009765625^7*(1-0.0009765625)^(20-7) = 6.483326146232712e-17
p8 = 20!/(8!12!)*0.0009765625^8*(1-0.0009765625)^(20-8) = 1.029853859983202e-19
p9 = 20!/(9!11!)*0.0009765625^9*(1-0.0009765625)^(20-9) = 1.342266353839299e-22
p10 = 20!/(10!10!)*0.0009765625^10*(1-0.0009765625)^(20-10) = 1.443297154665913e-25
p11 = 20!/(11!9!)*0.0009765625^11*(1-0.0009765625)^(20-11) = 1.2825887804726853e-28
p12 = 20!/(12!8!)*0.0009765625^12*(1-0.0009765625)^(20-12) = 9.403143551852531e-32
p13 = 20!/(13!7!)*0.0009765625^13*(1-0.0009765625)^(20-13) = 5.656451493707817e-35
p14 = 20!/(14!6!)*0.0009765625^14*(1-0.0009765625)^(20-14) = 2.7646390487330485e-38
p15 = 20!/(15!5!)*0.0009765625^15*(1-0.0009765625)^(20-15) = 1.0809927854283668e-41
p16 = 20!/(16!4!)*0.0009765625^16*(1-0.0009765625)^(20-16) = 3.3021529369146104e-45
p17 = 20!/(17!3!)*0.0009765625^17*(1-0.0009765625)^(20-17) = 7.59508466888531e-49
p18 = 20!/(18!2!)*0.0009765625^18*(1-0.0009765625)^(20-18) = 1.2373875315877011e-52
p19 = 20!/(19!1!)*0.0009765625^19*(1-0.0009765625)^(20-19) = 1.2732289258503896e-56
p20 = 20!/(20!0!)*0.0009765625^20*(1-0.0009765625)^(20-20) = 6.223015277861142e-61
Probability of at least one person in the stadium getting 10 heads
= p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 +
p11 + p12 + p13 + p14 + p15 + p16 + p17 + p18 + p19 + p20
= 0.01935110919485281
So the result is the same (the tiny difference is due to floating point precision), but as you can see the first calculation is slightly simpler for 20 people, never mind for 20000 ;)

Round up a decimal

I have a problem and i need your help!
Here is the code:
kg_lemons = float(input())
kg_sugar = float(input())
water = float(input())
total_lemon_juice = kg_lemons * 980 #in mililiters need to multiply by 1000
total_lemonade = total_lemon_juice + 5 * 1000 + (0.3 * kg_sugar)
cups_made = total_lemonade / 150
money_made = cups_made * 1.20
print(f'All cups sold: {cups_made:.2f}')
print(f'Money earned: {money_made:.2f}')
At then end, after I print it, it must shown the numbers:
All cups sold: 66
Money earned: 79.20
But I got:
All cups sold: 66.01
Money earned: 79.21
So I need to round it up to the second decimal (the lowest number). Should I use math.floor and, if so, how?

Azure Usage Scaled Rates Pricing

I try to calculate the price for a meterID that has scaled rates . I use this
as guide for the algorithm ( public static double computeRatedUsagePerMeter(Dictionary rates, double usage) )
https://github.com/PartnerCenterSamples/Commerce-API-DotNet/blob/master/Usage.cs
Comparing with the price from azure pricing calculator if i ask the price for quantity X in calculator it is equal to the price that i calculate from the above method but for quantity X - 1.
So i am confused if the method provided from Microsoft is complete or not , or maybe just a hint for the right direction.
private static decimal computeRatedUsagePerMeter(Dictionary<decimal, decimal> rates, decimal usage)
{
decimal total = Decimal.Zero;
if (rates.Count == 0)
return Decimal.Zero;
else if (rates.Count == 1)
return (usage * rates.Values.FirstOrDefault());
var remainingUsage = usage;
while (rates.Count > 0)
{
decimal LastKey = rates.Keys.Last();
if (remainingUsage > LastKey)
{
decimal LastKeyValue = Decimal.Zero;
if (rates.TryGetValue(LastKey, out LastKeyValue))
{
total = total + ((remainingUsage - LastKey + 1) * LastKeyValue); // remainingUsage - LastKey +1 because tiered pricing is exclusive
remainingUsage = LastKey - 1;
}
rates.Remove(LastKey);
}
else if (remainingUsage <= LastKey)
{
rates.Remove(LastKey);
}
}
return total;
}
{
"MeterId": "d23a5753-ff85-4ddf-af28-8cc5cf2d3882",
"MeterName": "Standard IO - Page Blob/Disk (GB)",
"MeterCategory": "Storage",
"MeterSubCategory": "Locally Redundant",
"Unit": "GB",
"MeterTags": [],
"MeterRegion": "",
"MeterRates": {
"0": 0.042165,
"1024": 0.0421650,
"51200": 0.0421650,
"512000": 0.0421650,
"1024000": 0.0379485,
"5120000": 0.0312021
},
"EffectiveDate": "2014-02-01T00:00:00Z",
"IncludedQuantity": 0.0
}
According to the method provided by the link above the price for quantity 1 = 0.084330 while azure pricing calculator gives 0.04
( the prices are in EUR )
And another example : les say 100 quantity.
method: 4.258665 EUR
Azure Calculator = 4.22 EUR
method for 99 quantity = 4.216500 which rounded is 4.22 EUR.
Also cannot check the prices < 1.00 lets say 0.5 quantity ( in this case its measured in GB so 0,5 GB is perfectly reasonable quantity ) cause pricing calculator doesn't allow decimal .
According to the method provided by the link above the price for
quantity 1 = 0.084330 while azure pricing calculator gives 0.04 ( the
prices are in EUR )
Looking at the code above, I believe there's an issue in the code itself. Essentially you're trying to find the price for 1 GB of storage which would fall under 0 - 1023 range or in other words the value of LastKey is 0. So when the following code executes:
total = total + ((remainingUsage - LastKey + 1) * LastKeyValue);
it gives you a total of 0.084330 (0 + (1 - 0 + 1) * 0.042165).
Also cannot check the prices < 1.00 lets say 0.5 quantity ( in this
case its measured in GB so 0,5 GB is perfectly reasonable quantity )
cause pricing calculator doesn't allow decimal.
I am sure someone from Microsoft would provide a proper answer as to why they designed the calculator the way it is designed.

While Loops: my program is intended to show growth of a bank account with user input

This is my code.
iBal = int(input('Enter your initial balance: '))
aPer = int(input('Enter an annual interest percentage: '))
fBal = int(input('Enter your desired final balance: '))
sum1 = iBal*(1 + aPer/100)
while sum1 < fBal:
print(format(sum1, '.2f'))
sum1 = iBal*(1+aPer/100)
iBal = sum1
print(format(iBal, '.2f'))
This is what's returned, with user input (iBal=500, aPer=4, fBal=550)
>>>520.00
>>>520.00
>>>540.80
>>>562.43
I need the first 520.00 to be 500.00 (the original inital balance entered), anyone see my mistake? Much appreciated.
You can do this, in a simplier way (your code is fine and correct too):
sum1 = iBal
while sum1 < fBal:
print(format(sum1, '.2f'))
sum1 = sum1 * (1 + aPer / 100.0)
print(format(sum1, '.2f'))
The way above is more readable, but if you want it even more concise, you can only use iBal and not sum1:
while iBal < fBal:
print(format(iBal, '.2f'))
iBal = iBal * (1 + aPer / 100.0)
print(format(iBal, '.2f'))
And you cant get 500 as output in the first case because you did not print out initial balance iBal before making calculations.

Fuzzy logic on price calculation

I am having one price calculation code in C#.There is particular sequence of calculating all the prices and taxes.how to apply fuzzy logic so that I will get related search of that particular price.That means if one of tax name is 'cost/kg'.So I should get all the names of taxes or prices which start from C or CO.
A simple way would be getting prices within some min/max bounds of what has been searched
double k = 0.1;
double min = searchedAmount - (searchedAmount * k);
double max = searchedAmount + (searchedAmount * k);
var results = products.Where(p => p.Price >= min && p.Price <= max);

Resources