Having a hard time grasping *= function - python

So if
balance = int(100)
balance *= 0.05
since balance is mutable should'nt that equal to 105? instead i just get 5.
and if i add another line of code such as
balance = int(100)
balance *= 0.05
balance *= 0.05
the output would be 0.25, essentially my variable is not carrying over and im just multiplying the end outcome to 5%
if i add
balance= int(100)
balance *= 0.05 + balance
i get 10005
I thought += or *= function could be used for an equation that would take a variable, do the equation then carry over the variable + the outcome as the new variable.
How do i do that for a multi step equation.

balance = int(100)
balance *= 0.05
is the same as
balance = int(100)
balance = balance * 0.05
Wouldn't you say that that's 5, not 105?
A *= B is just a shorthand for A = A * B.
Your third example is the same as:
balance= int(100)
balance = balance * (0.05 + balance)
Again, you're getting what I would think you'd expect from this code.
BTW, you don't need the int(). 100 by itself is a literal value of type 'int'. So the most concise way to state your first code block is:
balance = 100 * .05

Sorry for saying this but you have to first under the python or any programming language basics.
'+' is addition sigh
'*' is multiplication sign
A = 2 + 3
gives 5 as answer, and
A = 2 * 3 will give 6 as answer.
Secondly, '+=' , '*=' are shorthand where the operation's first value is the same space where you want to save the result.
like
A = 5
and want to add 3 in this same 'A'
A = A + 3 or can also be written as A += 3,
similarly for multiplication
A = 100
A = 100 * 0.05 can also be written as A *= 0.05
it will give A as 5
So, good luck.

Related

How can I increase a value by 5% monthly using a while loop?

I'm attempting to take a starting balance and increase the value by 5% each month. I then want to feed this new balance back into the equation for the next month. I've attempted to do this using a while loop but it doesn't seem to be feeding the new balance back in.
I'm using 60 months (5 years) for the equation but this can be altered
counter = 1
balance = 1000
balance_interest = balance * .05
while counter <= 60:
new_monthly_balance = (balance + balance_interest)*(counter/counter)
print(new_monthly_balance)
balance = new_monthly_balance
counter += 1
You never change balance_interest in the loop.
What do you intend to do with *(counter/counter)? This merely multiplies by 1.0, which is a no-op.
while counter <= 60:
balance *= 1.05
print(balance)
counter += 1
Better yet, since you know how many times you want to iterate, use a for:
for month in range(60):
balance *= 1.05
print(balance)
BTW, just what sort of finance has a constant 5% monthly increase???

Why is string not recognizing my output as an integer

balance = int(100)
balance *= 0.05 + balance
balance *= 0.05 + balance
balance *= 0.05 + balance
print (int(round ( balance, '.2f' )))
im trying to calculate what 100$ interest would be after 3 years compound interest.
I originally tried this
balance = 100
balance *= 0.05 + balance
balance *= 0.05 + balance
balance *= 0.05 + balance
print (format( balance, '.2f' ))
but my formatting caused the answer to be in the trillions instead of a 5 digit float.
You're multiplying the balances. Try this:
balance = int(100)
balance = balance * 0.05 + balance
balance = balance * 0.05 + balance
balance = balance * 0.05 + balance
print("{:.02f}".format(balance))
You have your operator precedence incorrect: the assignment operator is last. Thus, what you've done is
balance = balance * (0.05 + balance)
Instead, try one of the canonical ways to express interest:
rate = 0.05
balance += balance * rate
or
balance *= (1 + rate)
The parentheses aren't needed, but will help you read this.
Also, you might make a parameter (variable) for your repetition:
limit = 3
for year in range(limit):
balance *= 1 + rate
print("{:.02f}".format(balance))
You should pay attention to order of operations. balance *= 0.05 + balance will add 0.05 and balance before multiplying it to balance. What you'd want is balance = balance + balance * 0.05 or balance = balance * 1.05.
You can create a function to calculate compound interest to make it easier:
def comp_int(balance, rate, years):
return balance * (1 + rate)**years
balance = 100
rate = 0.05
years = 3
new_bal = comp_int(balance, rate, years)
print(f'{new_bal:.2f}')

TypeError: can't multiply sequence by non-int of type 'float', I can't figure out

This is that piece of code I wrote:
#This is the first ever piece of code I/'m Writing here
#This calculates the value after applying GST
#Example: here we are applying on a smartphone costing 10000
Cost = input('Enter the MRP of device here ')
Tax = 0.12
Discount = 0.05
Cost = Cost + Cost * float(Tax)
Total = Cost + Cost * float(Discount)
print(Total)
Whenever I try to execute the code it gives an exception after input:
TypeError: can't multiply sequence by non-int of type 'float'
There's a few weird parts here I'll try to break them down. The first is the one you are actually asking about which is caused by input returning a string, so you are effectively doing something like this. I'm going to lowercase the variable names to match python style
cost = "2.50"
tax = 0.12
#...
cost * tax # multiplying str and float
Fix this by wrapping the call to input with a call to float to convert the str
cost = float(input('Enter the MRP of device here '))
tax = 0.12
discount = 0.5
next you have these extra calls float(tax) and float(discount). Since both of these are floats already, you don't need this.
There is also a shorthand syntax for x = x + y which is x += y with these two things in mind, you can adjust your calculation lines:
cost += cost * tax
cost += cost * discount
print(cost)
raw input is as string,cast it into float
Cost = input('Enter the MRP of device here ')
Cost=float(Cost)
Tax = 0.12
Discount = 0.05
Cost = Cost + Cost * float(Tax)
Total = Cost + Cost * float(Discount)
print(Total)

Can anyone please explain this following programming questioƱ?

Question: Define a Python function named calculate_tax() which accepts one parameter, income, and returns the income tax. Income is taxed according to the following rule: the first $250,000 is taxed at 40% and any remaining income is taxed at 80%. For example, calculate_tax(100000) should return $100,000 * 0.40 = $40,000, while calculate_tax(300000) should return $250,000 * 0.40 + 50,000 * 0.80 = $140,000.
My question is simple, does the question ask for me to print out the whole math operation $100,000 * 0.40 = $40,000, or just the final answer$40,000?
It does say "should return $250,000 * 0.40 + 50,000 * 0.80 = $140,000," but all your function should actually return is the final value of 250000. The function should simply do the calculation and return the result. The equation is written out in order to help you create the function, not as an output requirement.
However, the best person to clarify assignments is the teacher who assigned them.
Is not it? like this:
def calculate_tax(income=250000):
tax = 0
if income <= 250000:
tax = income * 0.4
else:
tax = 250000 * 0.4 + (income - 250000) * 0.8
return int(tax)
print calculate_tax(100000) # 40000
print calculate_tax(300000) # 140000

in python, i have an int and want to substract it from a list

In python: how do I divide an int received by a user from a list while every time it runs in the for loop I need to divide the value I received from the round before in the next round?
This is my code:
a = input('price: ')
b = input('cash paid: ')
coin_bills = [100, 50, 20, 10, 5, 1, 0.5]
if b >= a:
for i in coin_bills:
hef = b - a
print (hef / i), '*', i
else:
print 'pay up!'
Example: a=370 b=500 ---> b-a=130
Now in the loop I will receive (when i=100) 1, and (when i=50) I will receive 2 but I want in the second round (when i=50) to divide 30 (130[=b-a]- 100[=answer of round 1*i]) by 50.
What do I need to change in the code?
Thanks!
You just need to subtract the amount of change you give back at each step from the total amount of change you're returning. It's much easier to see if you change your variable names to something meaningful:
price= int(raw_input('price: ')) # Use int(raw_input()) for safety.
paid= int(raw_input('cash paid: '))
coin_bills=[100,50,20,10,5,1,0.5]
if paid >= price:
change = paid - price
for i in coin_bills:
# Use // to force integer division - not needed in Py2, but good practice
# This means you can't give change in a size less than the smallest coin!
print (change // i),'*',i
change -= (change // i) * i # Subtract what you returned from the total change.
else:
print 'pay up!'
You could also clear up the output a bit by only printing the coins/bills that you actually return. Then the inner loop might look something like this:
for i in coin_bills:
coins_or_bills_returned = change // i
if coins_or_bills_returned: # Only print if there's something worth saying.
print coins_or_bills_returned,'*',i
change -= coins_or_bills_returned * i
OK, I'm assuming that you're trying to calculate change for a transaction using a number of types of bills.
The problem is that you need to keep a running tally of how much change you have left to pay out. I used num_curr_bill to calculate how many of the current bill type you're paying out, and your hef I changed to remaining_change (so it would mean something to me) for the remaining change to pay.
a= input('price: ')
b= input('cash paid: ')
coin_bills=[100,50,20,10,5,1,0.5]
if b>=a:
# Calculate total change to pay out, ONCE (so not in the loop)
remaining_change = b-a
for i in coin_bills:
# Find the number of the current bill to pay out
num_curr_bill = remaining_change/i
# Subtract how much you paid out with the current bill from the remaining change
remaining_change -= num_curr_bill * i
# Print the result for the current bill.
print num_curr_bill,'*',i
else:
print 'pay up!'
So, for a price of 120 and cash paid 175, the output is:
price: 120
cash paid: 175
0 * 100
1 * 50
0 * 20
0 * 10
1 * 5
0 * 1
0.0 * 0.5
One bill for 50 and one for 5 add up to 55, the correct change.
Edit: I'd go more sparingly on the comments in my own code, but I added them here for explanation so that you could more clearly see what my thought process was.
Edit 2: I would consider removing the 0.5 in coin_bills and replacing 1 with 1.0, since any fractional amounts will wind up being fractions of 0.5 anyway.

Categories

Resources