1

I'm trying to convert C++ code to python but I'm stuck

original C++ code

int main(void)
{
    int levels = 40;
    int xp_for_first_level = 1000;
    int xp_for_last_level = 1000000;

    double B = log((double)xp_for_last_level / xp_for_first_level) / (levels - 1);
    double A = (double)xp_for_first_level / (exp(B) - 1.0);

    for (int i = 1; i <= levels; i++)
    {
        int old_xp = round(A * exp(B * (i - 1)));
        int new_xp = round(A * exp(B * i));
        std::cout << i << " " << (new_xp - old_xp) << std::endl;
    }
}

python code

import math
from math import log
from math import exp

levels = 40
xp_for_first_level = 1000
xp_for_last_level = 1000000

B = log(xp_for_last_level / xp_for_first_level) / (levels - 1)
A = xp_for_first_level / (exp(B) - 1.0)

for i in range(1, levels):
    old_xp = round(A * exp(B * (i - 1)))
    new_xp = round(A * exp(B * i))
    print(i + " " + (new_xp - old_xp))

Any help is appreciated I can't seem to completely get it to work, when I fix one bug I'm creating another one.

icedwater
  • 4,701
  • 3
  • 35
  • 50
ScepT1c
  • 17
  • 1
  • 6

3 Answers3

5

Change the print line to:

print("%i %i" % (i, new_xp - old_xp))

Refer to this list of allowed type conversion specifiers for more informations.

Or use the new format method.

Dek Dekku
  • 1,441
  • 11
  • 28
  • what does %i do? this fixed it by the way thanks. (got to wait 8 minutes to tag as correct answer). – ScepT1c Jun 13 '13 at 07:25
  • It's a placeholder for an integer. You specify the actual value in the in the tuple that follows the percent sign. It works kinda like `printf` in C. – Dek Dekku Jun 13 '13 at 07:26
  • so print("%q %q" % (i, 5-2)) would print the value of i and 3? – ScepT1c Jun 13 '13 at 07:27
  • I doubt there is a `%q` though. – icedwater Jun 13 '13 at 07:29
  • Yep. The placeholders are not arbitrary letters. Each letter must match the type of the corresponding variable. I had the link with the list of allowed placeholders saved, but I can't seem to find it. Anyway, it's worth noting that the format function is the preferred way to format a string in Python 3. (The percent was supposed to be deprecated but it never happened, probably because for simple strings like these is more convenient.) – Dek Dekku Jun 13 '13 at 07:34
  • 1
    @DekDekku I think [this link](http://docs.python.org/2/library/stdtypes.html#string-formatting) helps. – icedwater Jun 13 '13 at 07:39
  • Thanks, that was it. I was probably looking in the Python 3 section... (I actually find another one in the tutorial and added in the original answer.) – Dek Dekku Jun 13 '13 at 07:45
3

For the last line, you can simply use:

print(i, new_xp - old_xp)

As @pfnuesel commented, you will need to adjust the range of your for loop slightly.

Finally, you don't need import math. You can replace the first 3 lines with:

from math import log, exp
grc
  • 22,885
  • 5
  • 42
  • 63
1

Depending on the version of python you are using, the cast to double in the C++ code

(double)xp_for_last_level / xp_for_first_level

might need to be taken into account in the python code. In python 3 you will get a float, in older python you can do

from __future__ import division

then xp_for_last_level / xp_for_first_level will give you a float.

See the discussion here

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62