[solved] Bank account

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

[solved] Bank account

#1 Post by The King »

Before I ask my question, I suppose I should try to clarify what I mean by bank account. In my game, I want the player to be able to make a savings account like in a bank or credit union, with an interest rate of 1%. For example, I save $1000, and after the first time interval, it should add 1% of that total to the bank account, which should be $10, if my math is correct, then it takes 1% of the previous total $1010, and add that on, making a total of $1021, and this keeps going and changing with how much money is in the account. My question is, is it possible for renpy to do calculations like that for a bank interest rate? If so, how do I impliment that? Below is the variable I use for the bank account, as I also want a separate variable for money the player has on their person.

%[player_bank]d

Please help me out, thank you. :)
Last edited by The King on Sun Nov 08, 2020 9:51 pm, edited 3 times in total.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Bank account

#2 Post by _ticlock_ »

Hi The King,

Sounds more like a math question.
player_bank = player_bank * (1 + interest_rate)^n, n -number of intervals

Code: Select all

python:
    interest_rate = 0.01
    n = 5 # number of intervals 
    player_bank = player_bank * pow(1+interest_rate,n)
    player_bank = int(player_bank)  # if you need only $ without cents

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Bank account

#3 Post by Vladya »

What you are talking about is called the phenomenon of "compound interest" in economics. And if you're making a game about a bank, or money, I think you'll find it interesting to read materials on that topic.
https://en.wikipedia.org/wiki/Compound_interest

User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

Re: Bank account

#4 Post by The King »

Okay, so here's how I implimented the code:

label banktest:
python:
interest_rate = 0.01
n = 5 # number of intervals
player_bank = 1000
"I think I'll open a bank account."
"Day one, started bank account."
"I have $1000."
"Day two."
$player_bank = player_bank * (1 + interest_rate)^n
"I now have $ %(player_bank)d."
return

When I got to the second to last line in that code, I got this traceback:

I'm sorry, but an uncaught exception occurred.

While running game code:
File "game/script.rpy", line 397, in script
$player_bank = player_bank * (1 + interest_rate)^n
File "game/script.rpy", line 397, in <module>
$player_bank = player_bank * (1 + interest_rate)^n
TypeError: unsupported operand type(s) for ^: 'float' and 'int'

-- Full Traceback ------------------------------------------------------------

Full traceback:
File "game/script.rpy", line 397, in script
$player_bank = player_bank * (1 + interest_rate)^n
File "C:\Users\King\Downloads\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "C:\Users\King\Downloads\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
exec bytecode in globals, locals
File "game/script.rpy", line 397, in <module>
$player_bank = player_bank * (1 + interest_rate)^n
TypeError: unsupported operand type(s) for ^: 'float' and 'int'

Windows-8-6.2.9200
Ren'Py 7.3.5.606
Codename X v0.1.0
Tue Nov 03 21:31:47 2020

What have I done wrong? Please help

User avatar
ghostclown
Regular
Posts: 28
Joined: Sun Oct 11, 2020 2:33 pm
Projects: R. I. P. Tour
itch: ghostclown
Contact:

Re: Bank account

#5 Post by ghostclown »

Exponents are calculated via ** or pow() in Python, not ^

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Bank account

#6 Post by _ticlock_ »

1) Vladya actually gave a pretty good link for more advanced compound interest. You might consider using something from it in the future.
2) If you have 1% interest each day, then after one day you need to use n = 1, i.e. number of intervals = 1.
3) As ghostclown mentioned, you need to use correct function for calculation. For your example, try this code:

Code: Select all

label banktest:
    $ day = 1
    "I think I'll open a bank account."
    "Day [day], started bank account."
    python:
        interest_rate = 0.01
        player_bank = 1000
        day_deposit = 1
    "I have $1000."
    $ day = 2
    "Day [day]."
    python:
        n = day - day_deposit
        player_bank *= pow(1+interest_rate,n)

    "I now have $ [player_bank]."
    return

User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

Re: Bank account

#7 Post by The King »

Okay, so I added the code exactly as it was written, and it worked. Then I decided to add another day afterward it to see if it kept working, and I was able to get that working as well. Could somebody tell me how to round the answer to two decimal places as well. That would be the last thing I need from this topic.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Bank account

#8 Post by _ticlock_ »

Code: Select all

player_bank = round(player_bank, 2)  # round to 2 decimals

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Bank account

#9 Post by _ticlock_ »

btw, you can google for simple python and renpy questions. It is fast and efficient. Also, it reduces the number of repetitive questions.

User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

Re: Bank account

#10 Post by The King »

I've been trying, but everything I do either results in some error, or just doesn't round the numbers. Here's my current code:

label banktest:
$ day = 1
"I think I'll open a bank account."
"Day [day], started bank account."
$player_bank = 1000
$player_bank = round(player_bank, 2) # round to 2 decimals
python:
interest_rate = 0.01
day_deposit = 1
"I have $1000.00."
$ day = 2
"Day [day]."
python:
n = day - day_deposit
player_bank *= pow(1+interest_rate,n)
"I now have $ [player_bank]."
$ day = 3
$ day_deposit +=1
"Day [day]."
python:
n = day - day_deposit
player_bank *= pow(1+interest_rate,n)

"I now have $ [player_bank]."
$ day = 4
$ day_deposit +=1
"Day [day]."
python:
n = day - day_deposit
player_bank *= pow(1+interest_rate,n)

"I now have $ [player_bank]."
return

I don't understand what to do. Please help if you can, thank you.

User avatar
Alex
Lemma-Class Veteran
Posts: 3098
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Bank account

#11 Post by Alex »

The King wrote: Fri Nov 06, 2020 11:55 pm ...
So, in this line you rounded the 'player_bank' variable

Code: Select all

$player_bank = round(player_bank, 2) # round to 2 decimals
Try to do the same after every calculation of 'player_bank' variable, before showing its value.

User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

Re: Bank account

#12 Post by The King »

Okay, so I added the player_bank rounding command before each display of the amount. But I wanted to add the trailing zero if the bank amount had one, so I tried using code from the forum: Adding a trailing zero to a number (or, formatting money), started by Funnyguts, the code in my game is below up until the end of day 2, the problem appearing right after I got to the Day 2 line:

label banktest:
$ day = 1
"I think I'll open a bank account."
"Day [day], started bank account."
$player_bank = 1000
python:
interest_rate = 0.01
day_deposit = 1
import decimal
decimal.getcontext().prec = 28
mynum = decimal.Decimal('1000.00') # This will be 99.99
mynum = decimal.Decimal('1000.00') # This will be 99.9899999999999948840923025272786617279052734375
player_bank = mynum.quantize(decimal.Decimal('.01')) # Default rounding
"I have $ [player_bank]."
$ day = 2
"Day [day]."
python:
n = day - day_deposit
player_bank *= pow(1+interest_rate,n)
player_bank = round(player_bank, 2) # round to 2 decimals
"I now have $ [player_bank]."

It displayed the correct answer for day 1's amount, but when I got to day 2, I got this:

I'm sorry, but an uncaught exception occurred.

While running game code:
File "game/script.rpy", line 404, in script
python:
File "game/script.rpy", line 406, in <module>
player_bank *= pow(1+interest_rate,n)
TypeError: unsupported operand type(s) for *=: 'Decimal' and 'float'

-- Full Traceback ------------------------------------------------------------

Full traceback:
File "game/script.rpy", line 404, in script
python:
File "C:\Users\King\Downloads\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "C:\Users\King\Downloads\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
exec bytecode in globals, locals
File "game/script.rpy", line 406, in <module>
player_bank *= pow(1+interest_rate,n)
TypeError: unsupported operand type(s) for *=: 'Decimal' and 'float'

Windows-8-6.2.9200
Ren'Py 7.3.5.606
Codename X v0.1.0
Sat Nov 07 21:06:56 2020

Is there a way to convert between decimal and float? Do I need to have float at all, or is there a way I can just use decimal?

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Bank account

#13 Post by _ticlock_ »

The error is because you have a different format of the number. player_account is decimal while you work with float.
If you just want to show 2 decimals try formating the output. The following code is calculations for 5 days in a row and also day 100 just for fun:

Code: Select all

label banktest:
    $ day = 1
    "I think I'll open a bank account."
    "Day [day], started bank account."
    $ player_bank = 1000.0
    python:
        interest_rate = 0.01
        day_deposit = 1
    "I have $ [player_bank:.2f]"
    $ day = 2
    "Day [day]."
    $ player_bank *= pow(1+interest_rate,1)
    "I now have $ [player_bank:.2f]."
    $ day = 3
    "Day [day]."
    $ player_bank *= pow(1+interest_rate,1)
    "I now have $ [player_bank:.2f]."
    $ day = 4
    "Day [day]."
    $ player_bank *= pow(1+interest_rate,1)
    "I now have $ [player_bank:.2f]."
    $ day = 5
    "Day [day]."
    $ player_bank *= pow(1+interest_rate,1)
    "I now have $ [player_bank:.2f]."
    $ day = 100
    "Day [day]."
    $ player_bank *= pow(1+interest_rate,100-5)
    "I now have $ [player_bank:.2f]."
Hope that is what you wanted. :)

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Bank account

#14 Post by Vladya »

One more example/sad story inspired by this post.

Code: Select all


init python:

    import math

    def _calculate_interest(_sum, rate, length=1., freq=1.):

        """
        :_sum:
            Principal sum.
        :rate:
            Nominal annual interest rate.
        :length:
            Overall length of time the interest is applied.
        :freq:
            Compounding frequency per unit of time.
            (Can be infinite).
        """

        _sum, rate, length, freq = map(float, (_sum, rate, length, freq))
        assert (freq > .0)

        if math.isinf(freq): 
            return (_sum * math.exp((rate * length)))
        return (_sum * ((1. + (rate / freq)) ** (length * freq)))

    def _input_positive_int(*args, **kwargs):
        kwargs["allow"] = tuple(map(unicode, xrange(10)))
        while True:
            value = renpy.input(*args, **kwargs)
            if not value:
                continue
            value = int(value)
            if value > 0:
                return value

    d = Character("Depositor")
    e = Character("Bank employee")

    _current_year = 2020
    my_money = 1.
    money_on_account = .0

label start:
    d "Yo! I want to make a deposit in your bank."
    e "Hey! It's great! How much do you want to invest?"
    $ deposit_sum = _input_positive_int("So... How much?")
    d "I want to invest $[deposit_sum:.2f]."
    if deposit_sum > my_money:
        e "You don't have such money. You look like a person who has only $[my_money:.2f]."
        $ deposit_sum = my_money
        e "For how long do you want to open a deposit?"
    else:
        e "Wow! Such money!!! For how long?"
    $ deposit_len = _input_positive_int("So... How long?", default="100")
    d "[deposit_len] years!!!"
    $ interest_rate = round(renpy.random.uniform(.005, .2), 4)
    e "Using the most modern technology with the codename \"renpy.random\", I calculated the best interest rate for you. You can make a deposit with a rate of [interest_rate:.2%%]!"
    $ _result_sum = _calculate_interest(deposit_sum, interest_rate, deposit_len)
    $ total_profit = _result_sum - deposit_sum
    e "Taking the above into account, in [deposit_len] years you will have $[_result_sum:.2f] on your deposit.{w} Are you happy?{w} You should be happy, because you will earn $[total_profit:.2f] by investing only $[deposit_sum:.2f]."
    $ my_money -= deposit_sum
    $ money_on_account += deposit_sum
    $ deposit_start_year = _current_year
    d "Sounds good, I'll go wait in your hall."
    e "Will you wait in the hall for [deposit_len] years?.."
    d "Yep! I think it will be fun! Let's wait together!"
    e "..."
    e "No."
    "..."
    while True:
        $ _current_year += 1
        "[_current_year]..."
        $ money_on_account = _calculate_interest(money_on_account, interest_rate)
        if (_current_year - deposit_start_year) >= deposit_len:
            d "I waited, sitting in the hall. During this time the bank went bankrupt, but if it was not, I would have taken my $[money_on_account:.2f] today. Okay, bad luck, I'll go home."
            return
        d "A year has passed. I have $[money_on_account:.2f] on my account."


User avatar
The King
Regular
Posts: 35
Joined: Fri Sep 25, 2020 12:37 pm
Completed: 0
Projects: LB
Contact:

Re: Bank account

#15 Post by The King »

Okay, it's finally working as I wanted it to. Thank you all again for your help, this should be it for this question. :)

Post Reply

Who is online

Users browsing this forum: Amazon [Bot]