使用Python函数计算复合兴趣

时间:2015-09-28 22:00:44

标签: python python-3.x

编写一个函数,在给定的年数后,计算具有给定初始余额和利率的银行账户余额。假设每年的利息复杂化。

我有错误" ValueError:不支持的格式字符'我' (0x49)在索引28"

到目前为止,这是我的代码。

def BankBalance():
    InputB = 1000
    return InputB
    print("Your initial balance is $1000")

def Interest():
    InputI = 0.05
    return InputI
    print("The rate of interest is 5%")

def CountNumber():
    InputN = float(input("Please enter the number of times per year you would like your interest to be compounded: "))
    return InputN

def Time():
    InputT = float(input("Please enter the number of years you need to compund interest for:"))
    return InputT

def Compount_Interest(InputB, InputI, InputT, InputN):
    Cinterest = (InputB*(1+(InputI % InputN))**(InputN * InputT))
    print("The compound interest for %.InputT years is %.Cinterest" %Cinterest)

B = BankBalance()
I = Interest()
N = CountNumber()
T = Time()
Compount_Interest(B, I, N, T)

3 个答案:

答案 0 :(得分:2)

您将如何做到这一点。

def main():
# Getting input for Balance
    balance = float(input("Balance: $ "))
# Getting input for Interest Rate
    intRate = float(input("Interest Rate (%) : "))
# Getting input for Number of Years
    years = int(input("Years: "))
    newBalance = calcBalance(balance, intRate, years)

    print ("New baance:  $%.2f"  %(newBalance))
def calcBalance(bal, int, yrs):
    newBal = bal
    for i in range(yrs):
        newBal = newBal + newBal * int/100
    return newBal

# Program run
main()

答案 1 :(得分:1)

您正在尝试将变量用作函数。 试试这个:

Cinterest = (InputB * (1+(InputI % InputN))**(InputN * InputT))

答案 2 :(得分:1)

Python和大多数其他编程语言都不假设两个相邻的数学表达式之间没有运算符意味着乘法。您缺少public class Dungeon1 implements Screen { public Texture tiles; public TextureRegion floor, wall, wallLeft, wallRight, tlCorn, trCorn, blCorn, brCorn, c1, c2, c3, c4; float wh = Gdx.graphics.getWidth(); float ht = Gdx.graphics.getHeight(); private SpriteBatch batch; private OrthographicCamera camera; private int[][] fg = Dungeon.fg; private int[][] bg = Dungeon.bg; public Dungeon1(NanoRealms game) { Dungeon d = new Dungeon(); tiles = new Texture(Gdx.files.internal("Map/tiles.png")); floor = new TextureRegion(tiles, 2 * 64, 0, Dungeon.tileSIZE, Dungeon.tileSIZE); wall = new TextureRegion(tiles, 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE); wallLeft = new TextureRegion(tiles, 0, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE); wallRight = new TextureRegion(tiles, 2 * 64, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE); tlCorn = new TextureRegion(tiles, 0, 64, Dungeon.tileSIZE, Dungeon.tileSIZE); trCorn = new TextureRegion(tiles, 2 * 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE); blCorn = new TextureRegion(tiles, 0, 3 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE); brCorn = new TextureRegion(tiles, 2 * 64, 3 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE); c1 = new TextureRegion(tiles, 3 * 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE); c2 = new TextureRegion(tiles, 4 * 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE); c3 = new TextureRegion(tiles, 3 * 64, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE); c4 = new TextureRegion(tiles, 4 * 64, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE); batch = new SpriteBatch(); camera = new OrthographicCamera(30, 30 * (Gdx.graphics.getHeight()/Gdx.graphics.getWidth())); camera.position.set(3000, 3000, 0); camera.zoom = 300; } public void render(float delta) { cameraInput(); Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); //// batch.begin(); batch.setProjectionMatrix(camera.combined); batch.enableBlending(); for (int x = 0; x < bg.length; x++) { float w = x * 64; for (int y = 0; y < bg[x].length; y++) { float h = y * 64; if (bg[x][y] == 1) { batch.draw(floor, w, h); } } } for (int x2 = 0; x2 < fg.length; x2++) { float w2 = x2 * 64; for (int y2 = 0; y2 < fg[x2].length; y2++) { float h2 = y2 * 64; if (fg[x2][y2] == 2) { batch.draw(wall, w2, h2); } else if (fg[x2][y2] == 3) { batch.draw(wallLeft, w2, h2); } else if (fg[x2][y2] == 4) { batch.draw(wallRight, w2, h2); } else if (fg[x2][y2] == 5) { batch.draw(tlCorn, w2, h2); } else if (fg[x2][y2] == 6) { batch.draw(trCorn, w2, h2); } else if (fg[x2][y2] == 7) { batch.draw(blCorn, w2, h2); } else if (fg[x2][y2] == 8) { batch.draw(brCorn, w2, h2); } else if (fg[x2][y2] == 9) { batch.draw(c1, w2, h2); } else if (fg[x2][y2] == 10) { batch.draw(c2, w2, h2); } else if (fg[x2][y2] == 11) { batch.draw(c3, w2, h2); } else if (fg[x2][y2] == 12) { batch.draw(c4, w2, h2); } } } batch.end(); //// } public void cameraInput() { if (Gdx.input.isKeyPressed(Keys.Q)) camera.zoom += 0.5; if (Gdx.input.isKeyPressed(Keys.E)) camera.zoom -= 0.5; if (Gdx.input.isKeyPressed(Keys.A)) camera.translate(-10, 0, 0); if (Gdx.input.isKeyPressed(Keys.D)) camera.translate(10, 0, 0); if (Gdx.input.isKeyPressed(Keys.S)) camera.translate(0, -10, 0); if (Gdx.input.isKeyPressed(Keys.W)) camera.translate(0, 10, 0); } } 与表达式其余部分之间的乘法运算符(*):

InputB