Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Python Programming

S_bal = float(input("How much money is your start balance ? $ "))

monthly_dep = float(input("How much additional money will you be putting into your account ? $ "))

int_rate = float(input("How much is your interest rate (1.75% enter 1.75) " ))

years = int(input("How many years are you going to invest? "))

 

rate = int_rate / 100

months = years * 12

m_int = S_bal * rate / 12

E_bal = S_bal + m_int + monthly_dep

S_bal = E_bal

 

c_mo = months + 1

while(c_mo <= months):

   m_int = S_bal * rate / 12

   E_bal = S_bal + m_int + monthly_dep

   S_bal = E_bal

   c_mo = months

 

print("Start Balance","Monthly Interest","End Balance")

print(S_bal,    m_int, E_bal)

print(round(S_bal,2),    round(m_int,2), round(E_bal,2))

 

dVNLy47A2kg5Ca7uW3QRlBM6ziZmWj7GVuKBuvMhKOxruHOLcMhEoF-H-nAG0-BG8RENSFzhzqb4wAb50kY4KkYemvdauHihOSDNipCSPIFtWTk-MxwVc4mVXCjV2Wcm5unqsqoE9hcb8aqafw

I was wondering what I'd have to fix in order to get the loop to display this, what coding would help fix the formatting issue, and if someone could help me check my math ?
I can fully understand how to write it out, but when it comes to coding I lack execution 

Link to post
Share on other sites

In Python, if you need a character repeated, you can do something like (' ' * n) and it'll put n spaces in for you. You can use this to programmatically format the spacing in the print out.

 

Also if this is a compound interest thing, then the formula if you want something more elegant is A = P * ( 1 + r / n ) ^ (n * t) where

  • A is the end result
  • P is what you're working with
  • r is interest rate
  • n is how many times per year interest is calculated
  • t is how many years

 

Link to post
Share on other sites
1 minute ago, M.Yurizaki said:

In Python, if you need a character repeated, you can do something like (' ' * n) and it'll put n spaces in for you. You can use this to programmatically format the spacing in the print out.

 

Also if this is a compound interest thing, then the formula if you want something more elegant is A = P * ( 1 + r / n ) ^ (n * t) where

  • A is the end result
  • P is what you're working with
  • r is interest rate
  • n is how many times per year interest is calculated
  • t is how many years

 

would there be a way to write the while loop in simpler, or should I use a for loop ?

 

Link to post
Share on other sites
1 minute ago, Haeking said:

would there be a way to write the while loop in simpler, or should I use a for loop ?

 

for this, I would use a for loop

 

Link to post
Share on other sites
Just now, BrokenProgram01 said:

for this, I would use a for loop

 

could you explain why you'd use the for loop in this situation 

Link to post
Share on other sites
4 minutes ago, Haeking said:

would there be a way to write the while loop in simpler, or should I use a for loop ?

 

A for-loop. If the number of iterations are known, for-loops tend to be cleaner looking.

 

Also I realized the program includes regular contributions, which there's a formula for that too so you don't need a loop. However I'm going to guess this is a homework assignment and it's asking you to use a loop.

Link to post
Share on other sites
Just now, M.Yurizaki said:

 

A for-loop. If the number of iterations are known, for-loops tend to be cleaner looking.

 

Also I realized the program includes regular contributions, which there's a formula for that too so you don't need a loop. However I'm going to guess this is a homework assignment and it's asking you to use a loop.

I can use a while or for loop in this either one is acceptable 

Link to post
Share on other sites

you could technically do either but I would find this easier to implement using a for loop

 

Link to post
Share on other sites
Just now, Haeking said:

I can use a while or for loop in this either one is acceptable 

This is just my preference, but here's how I decide which loop to use:

  • For loops are good if the number of iterations are known. In Python (2.7 anyway) I use something like for x in xrange(0, [some value], 1): which says what my iterator variable is, where I'm starting, where I'm ending, and how many places the iterator variable is jumping.
  • While loops are good if I don't know how many iterations are known. This is more useful for things like accepting user input with an explicit command to exit.

You can use a while-loop to do the same thing as a for-loop, but you end up writing more code that's all over the place. Like to do this:

for x in xrange(0, 10, 1):

I would need to do this for a while-loop

x = 0
while ( x > 10):
	# do stuff
    x += 1

The problems here are:

  • I have to remember to define what x is first
  • I have to remember to add the iteration at the end, otherwise it'll loop forever.
Link to post
Share on other sites
1 minute ago, M.Yurizaki said:

This is just my preference, but here's how I decide which loop to use:

  • For loops are good if the number of iterations are known. In Python (2.7 anyway) I use something like for x in xrange(0, [some value], 1): which says what my iterator variable is, where I'm starting, where I'm ending, and how many places the iterator variable is jumping.
  • While loops are good if I don't know how many iterations are known. This is more useful for things like accepting user input with an explicit command to exit.

You can use a while-loop to do the same thing as a for-loop, but you end up writing more code that's all over the place. Like to do this:


for x in xrange(0, 10, 1):

I would need to do this for a while-loop


x = 0
while ( x > 10):
	# do stuff
    x += 1

The problems here are:

  • I have to remember to define what x is first
  • I have to remember to add the iteration at the end, otherwise it'll loop forever.

understandable with the for loop
is there any way I could fix the formatting on this for columns, in the simplest fashion you'd know without over complicating it 

Link to post
Share on other sites
8 minutes ago, Haeking said:

understandable with the for loop
is there any way I could fix the formatting on this for columns, in the simplest fashion you'd know without over complicating it 

  • Define a maximum number of digits. Let's just say for the sake of simplicity, you aren't servicing billionaires so we could cap the maximum amount to $999,999,999.99
  • Define how much spacing you want between columns, assuming the maximum number of digits are in all the columns
  • You can then pad spaces from the maximum number of digits you are allowing by number to digits you have in the value.
    • So in this example, there are 11 digits, or 12 characters total if you're omitting the dollar sign and commas.
    • If you have 9999.99, this is seven characters, so create a string that has [Max digits] - [num charcters] spaces to start, then append the amount to the end.

Remember what I said about Python and its ability to repeat characters.

Link to post
Share on other sites

I was able to get your Code to work and get your desired solution but you should really try to debug and see if you can debug before you look at the Spoiler

Spoiler

S_bal = float(input("How much money is your start balance ? $ "))
monthly_dep = float(input("How much additional money will you be putting into your account ? $ "))
int_rate = float(input("How much is your interest rate (1.75% enter 1.75) "))
years = int(input("How many years are you going to invest? "))

rate = int_rate / 100
months = years * 12
m_int = S_bal * rate / 12
E_bal = S_bal + m_int + monthly_dep
c_mo = months + 1

print("Beginning","\t","Monthly","\t", "End")
print("Balance","\t","Interest","\t","Balance")

for i in range(0,months):
    m_int = S_bal * rate / 12
    E_bal = S_bal + m_int + monthly_dep
    print(round(S_bal, 2),"  \t", round(m_int, 2),"   \t", round(E_bal, 2))
    S_bal = E_bal

 

Edit: spacing does work

Link to post
Share on other sites
2 minutes ago, BrokenProgram01 said:

I was able to get your Code to work and get your desired solution but you should really try to debug and see if you can debug before you look at the Spoiler

  Reveal hidden contents

Edit: spacing does work

 

I did my spacing the lazy way you should do it the way that M.Yurizaki is referring to as it is a much better implementation

Link to post
Share on other sites
Just now, BrokenProgram01 said:

I did my spacing the lazy way you should do it the way that M.Yurizaki is referring to as it is a much better implementation

My loop wasn't properly written in the situation, and my defining variables wouldn't have shown up since the lines in a way conflicted
If that makes sense,
Is there a website you'd recommend so I can teach myself python and get better at the program ? 

Link to post
Share on other sites
1 hour ago, Haeking said:

My loop wasn't properly written in the situation, and my defining variables wouldn't have shown up since the lines in a way conflicted
If that makes sense,
Is there a website you'd recommend so I can teach myself python and get better at the program ? 

Codecdamy has a nice put on course it's what I used to get started.

 

You should look I to python string formatting, I know you can set the fan space ect but it's been a while since I used python.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites
11 hours ago, Haeking said:

My loop wasn't properly written in the situation, and my defining variables wouldn't have shown up since the lines in a way conflicted
If that makes sense,
Is there a website you'd recommend so I can teach myself python and get better at the program ? 

The way I have learned coding is by reading some example code and figuring out what is happening by looking at the python reference sheets but if you want a more structured way I would recommend code academy or Lynda

Link to post
Share on other sites
15 hours ago, Haeking said:

Is there a website you'd recommend so I can teach myself python and get better at the program ? 

You can some of the websites the other users have suggested, if you want to get more serious and commit more time to learning Python I have personally found this course very enjoyable. 
https://www.udemy.com/complete-python-bootcamp/

 

If you're still trying to figure it out if you want to learn more than websites like CodeAcademy and other online resources will help you out.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×