Aug 22, 2017 06:32 PM
(This post was last modified: Aug 22, 2017 07:25 PM by Truhmp.)
So my first day of intro to programming we are going to be learning python which im glad. I was able to easily do the first two assignments with breeze since my instructor was there to teach me about what was needed in those.
My first day was durring the eclipse and everyone wanted to go outside so class was called early and I was not taught anything about how to do this next Exercise.
If someone can simply just show me what they would do using python 3 I can easily understand why you did it and that will be enough for me.
Write a program that prints the balance of an account after the first, second and third year. The account has an initial balance of $1000 and earns 5 percent interest.
Please can someone help me out, Ive spent 6 hours going through my textbook and they do not show me this either. Online has a few option for using java but im using python 3. Thank you for your time.
EDIT: This is what ive came up with so far but still failing at figuring out how to calculate after Year One: and so on.
#####Account Balance#####
print(" Account Balance after 3 years and earning 5 percent interest");
print()
print()
print("Year One:")+(1000*1.05)
print(1000*1.05*1.05);
print(1000*1.05*1.05*1.05);
print()
print()
input()
How do I make it look like Year One: 1050, then I can do the rest.
Aug 22, 2017 06:35 PM
(This post was last modified: Aug 22, 2017 06:35 PM by Bαker.)
Why not do something like this, (Quick throw together on mobile)
A = $1000
B = A .05
C = B .05
- - -
Obviously you'd need to actually code it up but maybe this would help. (FYI I am not a python guy, im a front dev type guy so this is just a basic example)
Aug 22, 2017 08:56 PM
(This post was last modified: Aug 23, 2017 03:21 PM by Lukos1234.)
I'm new to python but try this :
PHP Code
<?php def calculbalance(): years = int(input("Please precise how much years you will stay with our bank so we can calculate your balance\n")) currentbalance1 = int(input("Please what's your current balance ?\n")) Interest=int(input("Please what's your interest rate: \n")) for i in range(years): newbalance = currentbalance1*Interest print("For year " + str(i+1) + " here's your account balance : $" + str(newbalance) ) #I think you're looking for this only currentbalance1=newbalance
calculbalance()
Here's a much simpler one :
PHP Code
<?php def calculbalance(years,balance,interest): # interest in % print("with an interest of " + str(interest)+ " % and an initial balance of $" + str(balance)) interest = interest / 100
for i in range(years): balance = balance*(1+interest) print("for year " + str(i+1) + " your balance is $" + str(balance))
calculbalance(3,1000,5)
which could give us:
with an interest of 5 % and an initial balance of $1000
for year 1 your balance is $1050.0
for year 2 your balance is $1102.5
for year 3 your balance is $1157.625
# Just a simple function that asks for years , interest and beginning balance in account.
# interest accumulates each year , if each month I can rewrite the function.
#if anyone got the solution for converting input result into integer directly please help.
DO NOT DEAL WITH THIS MEMBER
Aug 22, 2017 09:03 PM
(This post was last modified: Aug 22, 2017 09:16 PM by Vana.)
I'd write a function:
PHP Code
<?php def calc(balance, year): while year > 0: balance *= 1.05 year -= 1 return balance
And just call it for each print:
PHP Code
<?php initial = 1000 print("First year: {0}".format(calc(initial, 1))) print("Second year: {0}".format(calc(initial, 2))) print("Third year: {0}".format(calc(initial, 3)))
You need to use the "format" function to add it to the strings as you can't directly concentrate numbers and strings in Python.
And if you need a variable number of years...
PHP Code
<?php years = int(input("Years to show: ")) for year in range(1, years + 1): print("Year {0}: {1}".format(year, calc(initial, year)))
(Aug 22, 2017 09:03 PM)Vana Wrote: years = input("Years to show: ")
for year in range(1, years + 1):
print("Year {0}: {1}".format(year, calc(initial, year)))
[/php]
Does this work even without converting the variable "years" to int ? because I read that input function gives you a string.
DO NOT DEAL WITH THIS MEMBER
Aug 22, 2017 09:17 PM
(This post was last modified: Aug 22, 2017 09:17 PM by Vana.)
(Aug 22, 2017 09:15 PM)Lukos1234 Wrote: Does this work even without converting the variable "years" to int ? because I read that input function gives you a string.
You're right, in Python 2.x `input()`will interpret it as an int but not in Python 3. Fixed my code.
Using OOP is much cleaner and easy to read.
PHP Code
<?php class Account(): init_balance = 1000
def increase(self,num_years): self.balance = Account.init_balance*1.05**num_years return self.balance
a = Account() [a.increase(i) and print("Year {}: ${:.3f}".format(str(i), a.balance)) for i in range(1,4)]
Output:
PHP Code
<?php Year 1: $1050.000 Year 2: $1102.500 Year 3: $1157.625
DO NOT DEAL WITH THIS MEMBER
I think you have enough here to complete this task. But consider what data type would best fit the role for USD and how it should be rounded. When multiplying, if you round fractions of a penny off an interest rate, then that could be considered fraud.
You may want to try and not overthink assignments. Consider the code expected for you is beginner-level. Save yourself the time. I over complicated my code in school trying to impress, but it really did nothing for me except for being investigated for plagiarism. No one was ever impressed. Instead, just do what the assignment asks perfectly.
Wow thank you everyone for the responses. I was able to complete the exercise with this information. This is alot of fun, although im not doing anything too complex yet im having alot of fun with the basics.
Aug 24, 2017 12:48 AM
(This post was last modified: Aug 24, 2017 12:48 AM by Lukos1234.)
(Aug 23, 2017 07:42 PM)Truhmp Wrote: Wow thank you everyone for the responses. I was able to complete the exercise with this information. This is alot of fun, although im not doing anything too complex yet im having alot of fun with the basics.
wait till you get to files handling :D
(Aug 24, 2017 12:48 AM)Lukos1234 Wrote: wait till you get to files handling :D
Files are easy
PHP Code
<?php with open('file','r') as f: For line in f: Print (f)
Wrote on my phone so spacing will be wrong.
¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
DO NOT DEAL WITH THIS MEMBER
Aug 25, 2017 02:44 PM
(This post was last modified: Aug 25, 2017 02:58 PM by Allelujah.)
Simple solution:
PHP Code
<?php import time
###Varibles### Principle = 1000 YearOne = Principle * 1.05 YearTwo = YearOne * 1.05 YearThree = YearTwo * 1.05 #YearFour #YearFive
###Print strings### print('The account has an initial balance of $1,000.00 and earns 5 percent interest.') print()
print('The account after the first year will have a balance of: $' + str(float(YearOne)) + ' after interest.') print()
print('The account after the second year will have a balance of: $' + str(float(YearTwo)) + ' after interest.') print()
print('The account after the third year will havea balance of: $' + str(float(YearThree)) + ' after interest.') print()
print('This is the final balance: $' + str(float(YearThree))) print()
time.sleep(5)
Aug 25, 2017 03:17 PM
(This post was last modified: Aug 25, 2017 03:21 PM by vorticalbox.)
(Aug 25, 2017 02:44 PM)Allelujah Wrote: Simple solution:
PHP Code
<?php import time
###Varibles### Principle = 1000 YearOne = Principle * 1.05 YearTwo = YearOne * 1.05 YearThree = YearTwo * 1.05 #YearFour #YearFive
###Print strings### print('The account has an initial balance of $1,000.00 and earns 5 percent interest.') print()
print('The account after the first year will have a balance of: $' + str(float(YearOne)) + ' after interest.') print()
print('The account after the second year will have a balance of: $' + str(float(YearTwo)) + ' after interest.') print()
print('The account after the third year will havea balance of: $' + str(float(YearThree)) + ' after interest.') print()
print('This is the final balance: $' + str(float(YearThree))) print()
time.sleep(5)
Why didn't you just do this in a for loop updating a variable? Would say you writing it out three times. Also why are you making it a float then converting to a string? It's already a float.
¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
Code def future_value(principle, interest, years = 1):
return principle * (1 + interest) ** years
DO NOT DEAL WITH THIS MEMBER
(Aug 25, 2017 03:17 PM)vorticalbox Wrote: Why didn't you just do this in a for loop updating a variable? Would say you writing it out three times. Also why are you making it a float then converting to a string? It's already a float.
Because I'm still learning and I figure he is too from his post. Thanks for your input. I actually thought about a loop after I had posted.
|