What I'm trying to do in the while loop is iterate the payments by an integer of 10 so that if that integer (g) fails to get the CBalance <= 0 within a 12 months period then all of the variables reset except for g, which goes up by 1.
Balance = float(raw_input('Enter Balance '))
Rate = float(raw_input('Enter interest rate '))
monthlyI = Rate/12
month = 0
g = 1
Payment = 10
CBalance = Balance
while CBalance > 0:
Payment = Payment*g
month += 1
CBalance = CBalance *(1+ monthlyI)- Payment
if month > 12:
month = 0
g += 1
Cbalance = Balance
I think I finally understand what your question is about and what's causing the problem -- namely a simple misspelling of a variable name. To fix it, just change the last line of the statements following the if in your while loop from:
if month > 12:
month = 0
g += 1
Cbalance = Balance
to:
if month > 12:
month = 0
g += 1
CBalance = Balance # note corrected spelling of variable name on left
Which explains why all the values weren't being reset. It would have been helpful if you explicitly mentioned which variable it was in your question if you knew it. Anyway, this sort of thing is more likely to happen when one uses Capitalized and mixedCase variable names as you are doing.
Many programmers try to avoid them for that reason, is especially with languages like Python where you generally don't have to declare variables before using them. You might want to check out the Naming Conventions section of PEP 8's style guidelines.
What I'm trying to do in the while loop is iterate the payments by an integer of 10 so that if that integer (g) fails to get the CBalance <= 0 within a 12 months period then all of the variables reset except for g, which goes up by 1.
I think what's happening is that each time you run this while you will get:
Payment = 10 * 1 //First while payment = 10
2nd time
Payment = 10 * 1 //payment = 10 again.
Which results in:
CBalance = CBalance * (1 + monthlyI) - 10
Which can never won't go to a negative value, which is needed to end the loop?
While you probably want:
counter = 1;
while CBalance > 0:
Payment = Payment*counter
month += 1
counter += 1
CBalance = CBalance *(1+ monthlyI)- Payment
if month > 12:
month = 0
counter = 1
g += 1
Cbalance = Balance
Related
I want to make a code that would propose a customer one or more of products to add to his shop list if he has total cart less than available budget (for example, he bought products for 120 dollars, and program should ask him if he want to add one or a couple of products (for example for 20 and 30 dollars) in list so budget gets close to 150 dollars limit)
My problem here is that somehow i += 1 creates an unwanted input() process which i cannot explain
I am a newbie in Python so maybe someone can propose the solving of problem or even better version of my code, i'll be greatful for every help!
https://ibb.co/vJwhMw0 link for image here
inp = 'bar'
product_list = ['bar', 'bread', 'milk', 'coconut']
product_price = [10, 24, 30, 25]
indx = product_list.index(inp)
price = product_price[indx]
budget = 150
total = 120
proposal_list = ''
i = 0
while total < budget:
if (product_price[i] + total) <= budget:
total += product_price[i]
proposal_list += product_list[i]
i = i + 1
print (f'You can also add: {proposal_list}')
There is some problems with your code. No unwanted input() is created it's just that your while loop is infinite because i never changes because the condition is never true at some point.
while total < budget:
if i >= len(product_price):
break
elif (product_price[i] + total) <= budget:
total += product_price[i]
proposal_list += product_list[i]
i += 1
You code got inside infinite loop right after this line:
while total < budget:
Once the code inside this line is executed
if (product_price[i] + total) <= budget:
total become equal 130. Condition total < budget is still true but condition
if (product_price[i] + total) <= budget:
is not.
You need to exit the while loop. You can't do it until you update total.
Good morning, It's been a long time since I've played around with python and I seem to have forgotten a fair bit.
I'm trying to make a repayment credit card repayment calculator, I've got it to work but I'm trying to improve it. currently It works if I fill in a payment amount and that will tell me how long it will take to pay, However I would like to fill in number of payments instead and then it tell me how much I need to pay. This is my code, can someone please point me in the right direction
original_amount = amount = 500
repayment = 1
interest = 30.34
total_interest = 0
number_of_payments = 8
def calculate_interest(old_balance):
global interest
global monthly_interest
global total_interest
monthly_interest_percent = interest / 100 / 12
monthly_interest = old_balance * monthly_interest_percent
new_balance = old_balance + monthly_interest
total_interest += monthly_interest
return new_balance
count = 0
while amount > 0:
card_value = calculate_interest(amount)
amount = card_value - repayment
count += 1
if count > number_of_payments:
repayment += 1
I think your problem is this:
if count > number_of_payments:
repayment += 1
repayment is the amount you are repaying but I don't think that's what you want to increment here, you probably want to increment the number_of_payments.
my problem is that the 'month' value increments once to month = 1, then stays there the whole time, causing an infinite loop. How do I get this to change every time through the loop? I know I'm probably missing something extremely simple.
def rem_bal(balance, annualInterestRate, monthlyInterestRate):
month = 0
while month <= 12:
monthly_interest = (annualInterestRate) / 12.0
minimum_monthly = (monthlyInterestRate) * balance
monthly_unpaid= (balance) - (minimum_monthly)
updated_balance = round(((monthly_unpaid) + (monthly_interest * monthly_unpaid)), 2)
month =+ 1
print("Month " + str(month) + "Remaining balance: " + str(updated_balance) + " .")
balance = updated_balance
return balance
month += 1
not
month = +1
which is just
month = 1
It needs to be month += 1 not month =+ 1; the latter is just plain assignment rather than incrementing the value of month (i.e., assigning month to +1/1).
BTW, this is not how you write a code in python.
Why parentheses around almost everything?
Why recalculate monthly_interest over and over, when it doesn't change?
Using a while loop for this isn't pythonic. You should rather use
for month in range(13):
month = month+1 - tried this works
I'm having trouble solving problem 2 from this page
Here
Here's my code:
#Problem set 1 b
out_bal = float(raw_input("Enter the outstanding balance on your credit card: "))
ann_interest = float(raw_input("Enter the annual interest rate as a decimal: "))
min_pmnt = 10
months = 1
prev_bal = out_bal
month_interest = ann_interest / 12.0
updated_bal = prev_bal * (1 + month_interest) - min_pmnt
while out_bal > 0:
for i in range(1,13):
out_bal = updated_bal
prev_bal = out_bal
months += 1
if out_bal <= 0:
break
else:
min_pmnt = min_pmnt + 10
months = 0
print out_bal
print "RESULT"
print "Monthly payment to pay off debt in 1 year: $", min_pmnt
print "Number of months needed: ", months
print "Balance: ", round(out_bal, 2)
I want to take 1200 and .18 for the first two inputs, respectively. However when I do this the program gets caught in my while loop and keeps printing 1208.00.
When I read the code to myself it seems like it should be working. But I think I'm not using the prev_bal variable correctly.
Also, it's performing the math the way I expect it to the first time it goes through the loop but then it seems like it's not adding 10 to min_pmnt and going through the loop again. It seems like it's just using 10 over and over.
How can I write this so it does actually add 10 and perform the loop again?
Thanks!
Your problem is that updated_bal isn't changing, yet you are setting out_bal to it each time you iterate, and then conditioning the loop on out_bal becoming reduced in value.
It looks to me like you are thinking that updated_bal changes on each loop iteration, based on its component parts. In order for that to work, you would need to instead use a function that calculates an updated balance each time around.
Ok, I am trying to make a simple program to kinda test how well i am learning things, I have come to a point where it is getting very large as I want the program to store data in sections (Day1,Day2....ect) so i tried to assign it to read the current Day count (Num_Days = ) but it doesnt seem to like this. I made a small test loop to try and test out if i could do this and have gotten stuck even though the logic looks good to me. I tried to do some searches but as i dont know what I am trying to do is even called I havent gotten very far. What I want to do is have the loop read the Num_Days and give the Days() the count and assign it to that day through 'n'.
Num_Days = 0
Total = 0
Data = 0
Day1 = 0
Day2 = 0
Day3 = 0
def Start_Work(x):
while Num_Days < 3:
Num_Days += 1
print "This is Day:",Num_Days
n = Num_Days
Total = +20
Day(n) += Total
else:
print "failed"
x = str('start')
I also made a dpaste on it as it is easier for me to look at it that way then in the full black: http://dpaste.com/1398446/
In order to clear up apparently where I lost some people with thinking that I am just trying to make a single loop that sits by its self I am going to put up what I am trying to use this for. This program is functioning the way I have wanted it to, the problem being that if i wanted o make it bigger it would get to be very long.
NumDays = 0
TotalOut = 0
Day1Tot = 0
Day1_RepsCnt = 0
Day4 = 0
def Work_Out(x):
while x == 1: ##crunches
NumDays = 0
TotalOut = 0
Day1Tot = 0
Day1_RepsCnt = 0
Day1_WghtCnt = 0
Day4 = 0
while NumDays < 3:
Day1_Wght = float(raw_input("How much weight did you use?"))
Day1_Reps = float(raw_input("How many reps did you do?"))
Day1_Sets = float(raw_input("How many sets were done?"))
Day1 = Day1_Wght * Day1_Reps * Day1_Sets
NumDays += 1
print "Day:",NumDays
print "Your total output is:",Day1
Day1_RepsCnt += Day1_Reps
Day1_WghtCnt += Day1_Wght
Day1Tot += Day1
TotalOut += Day1
if NumDays == 3:
print "Your total output for 3 days is:",TotalOut
print "Lets increase the Weight to",(Day1_Wght + 10)
print "Increase the Weight for days 4-6"
while NumDays >= 3 and NumDays <6:
Day4_Wght = float(raw_input("How much weight did you use?"))
if Day4_Wght <= (Day1_WghtCnt/3):
print "You need to increase your total output, add 10 pounds."
break
Day4_Reps = float(raw_input("How many reps did you do?"))
Day4_Sets = float(raw_input("How many sets were done?"))
Day4 += Day4_Wght * Day4_Reps * Day4_Sets
NumDays += 1
print "Day:",NumDays
if Day4_Wght <= (Day1_WghtCnt/3):
print "Re-enter totals once you have added the additional weight."
else :
print "Your total output was:",Day4
while x == 2: ##Benching
NumDays = 0
TotalOut = 0
Day1Tot = 0
Day1_RepsCnt = 0
Day4 = 0
while NumDays < 3:
Day1_Wght = float(raw_input("How much weight did you use?"))
Day1_Reps = float(raw_input("How many reps did you do?"))
Day1_Sets = float(raw_input("How many sets were done?"))
Day1 = Day1_Wght * Day1_Reps * Day1_Sets
NumDays += 1
print "Day:",NumDays
print "Your total output is:",Day1
Day1_RepsCnt += Day1_Reps
Day1Tot += Day1
TotalOut += Day1
if NumDays == 3:
print "Your total output for 3 days is:",TotalOut
print "Lets increase the Reps to",(Day1_Reps + 10)
print "Increase reps for days 4-6"
while NumDays >= 3 and NumDays <6:
Day4_Wght = float(raw_input("How much weight did you use?"))
Day4_Reps = float(raw_input("How many reps did you do?"))
if Day4_Reps <= (Day1_RepsCnt/3):
print "You need to increase your total output, do 10 more Reps."
break
Day4_Sets = float(raw_input("How many sets were done?"))
Day4 += Day4_Wght * Day4_Reps * Day4_Sets
NumDays += 1
print "Day:",NumDays
if Day4_Reps <= (Day1_RepsCnt/3):
print "Re-enter totals once you have completed the additional reps."
else :
print "Your total output was:",Day4
print "Available work outs in this version: crunches, benching"
Input = raw_input("What type of Work Out did you do?")
if Input.lower() == str('crunches'):
Work_Out(1)
if Input.lower() == str('benching'):
Work_Out(2)
else:
print "Failed"
And yes I understand that this needs to be cleaned up, but I have other ideas of what i want to throw in there and things i want to rearrange, but right now its just trying to figure out how I can break this into weekly cycles, and break each week into daily cycles, so i started with trying to get through one week and figure out that it would be very difficult just trying to get past 2 days so i broke it into 2 parts instead of 6 days. Any advise is welcome.
welcome to Python!
One of the beauties of Python is that the vast majority of Python programmers try to do things in the most "Pythonic" way when possible, so the first step I am going to take is to rename your variables and such according to PEP-8 standards. In other words, class names would be capitalized camelcase, but standard variables should be lowercase underscore-separated.
Second, if you ever find yourself naming variables with numbers like day1, day2, day3 etc, stop yourself and realize how unmaintainable that would be if you ever had to extend your program to work with 100 days (or 1,000, or 10,000, you get the point). Instead, you can just use a list called days and add as many as you need to it according to some configuration variable (e.g. total_days). For example:
total_days = 3
days = []
for _ in range(total_days):
days.append(0)
Or, use a list comprehension to be more Pythonic:
total_days = 3
days = [0 for _ in range(total_days)]
With these implementations all you'd have to do to add more days is to change the value of total_days. With all this in mind, let's try to reproduce your program:
# method definition
def start_workouts(total_days, init_workout, workout_increase):
workouts = [(init_workout + day * workout_increase) for day in range(total_days)]
return workouts
# method execution (3 days, starting at 100, increasing 20 each day)
my_workouts = start_workouts(3, 100, 20)
# my_workouts == [100, 120, 140]
# my_workouts[0] is "Day1"
# my_workouts[1] is "Day2"
# my_workouts[2] is "Day3"
So notice we moved some variable declarations to be passed in as arguments to your method. This way you can easily change the criteria for your workouts depending upon various circumstances that you might decide later. Also, we reduced all the calculations down to be part of a single list comprehension! (Isn't Python awesome?)
I hope I understood what you were trying to do correctly and that this helps you out. Let me know if you have any questions.
Might not be the best idea to set Days explicitly, as #blakev says, just use a list.
Num_Days = 0
Total = 0
Data = 0
Days = []
def Start_Work():
while Num_Days < 3:
Num_Days += 1
print "This is Day:",Num_Days
Total += 20
Days[Num_Days] = Total
else:
print "failed"
Start_Work() # call the function
You should get output that looks like
[20, 40, 60]
Your code does not look as correct Python code. Below are correction:
#-------------------
#while (condition):
# #commands
#-------------------
#for i in xrange(3):
# #commands
#-------------------
#Examples:
Num_Days = 0
Total = 0
Day = [0,0,0]
while Num_Days<3:
Num_Days += 1
print "This is Day:",Num_Days
Total += 20
Day[Num_Days-1] += Total
print Day
>>>
This is Day: 1
This is Day: 2
This is Day: 3
[20, 40, 60]
or better use:
Total = 0
Day = [0,0,0]
n = 3
for i in xrange(n):
print "This is Day:",i+1
Total += 20
Day[i] += Total
print Day
>>>
This is Day: 1
This is Day: 2
This is Day: 3
[20, 40, 60]