0

I am begginer in python and this homework require me to get the future value of an investment.

p = raw_input("[How much did you invest?]:")
r = str(raw_input("[How much is the interest rate?]:"))
n = raw_input("[How long have you been investing?]:")
future_value = p*(1+1)**n
print "\n\n\tYour future value of your investment is: %s\n" % future_value

Error Code:

unsupported operand type(s) for ** or pow(): 'int' and 'str'

Any Help?

2 Answers2

1

You need to cast input to int because raw_input function returns a string

If you type help(raw_input) in interactive terminal, you should see the definition:

raw_input(...)
    raw_input([prompt]) -> string

Fixed code:

p = int(raw_input("[How much did you invest?]:"))
r = float(raw_input("[How much is the interest rate?]:"))
n = int (raw_input("[How long have you been investing?]:"))
future_value = p*(1+1)**n
print "\n\n\tYour future value of your investment is: %s\n" % future_value
Mingyu
  • 31,751
  • 14
  • 55
  • 60
  • It worked with (1+1)**n. When I changed it to (1+r)**2 I had the same error message. – user2803287 Sep 22 '13 at 04:12
  • In your original code, `r` was not used. If you need to add `r` into the formula, you need to cast `r` to `int` as well :D Code above has been updated accordingly – Mingyu Sep 22 '13 at 04:14
  • [How much did you invest?]:1000 [How much is the interest rate?]:.08 Traceback (most recent call last): File "C:\*\*\Desktop\CSC130-Assignment-2\Asn2-4.py", line 2, in r = int(raw_input("[How much is the interest rate?]:")) ValueError: invalid literal for int() with base 10: '.08' – user2803287 Sep 22 '13 at 04:17
  • This is what I had. And yes it was my mistake when i typed 1+1 instead of r :D – user2803287 Sep 22 '13 at 04:18
  • You need to cast it to `double` or `float` if you enter .08. See my update – Mingyu Sep 22 '13 at 04:19
  • @Mingyu, `double()` isn't part of Python. While Python floating-point numbers are C doubles, in Python it's spelled `float()`. – Tim Peters Sep 22 '13 at 04:22
1

The error message is telling you you're trying to raise integer to a string power. That must be this part of the code:

(1+1)**n

Indeed, 1+1 is an integer (it's 2 - if 2 is what you want, why not write 2 instead of 1+1?).

So what's n? n was obtained from a raw_input() call. And, indeed, raw_input() always returns a string. If you want to change that string to an integer (you do), do this instead:

n = int(raw_input("[How long have you been investing?]:"))
Tim Peters
  • 67,464
  • 13
  • 126
  • 132
  • Yes you are right actually .it is (1+r)**n. It was mistake when i typed 1+1. I still have the same error unsupported operand type(s) for +: 'int' and 'str' – user2803287 Sep 22 '13 at 04:08
  • That's not the same error! The first error you reported was about unsupported operands types for `**`. Now you're getting one for `+`. That's because `r` is a string: **everything** returned by `raw_input()` is a string. If you want `r` to be an integer, you need to add an `int()` call, same as before. Or if you want `r` to be a floating-point number, a `float()` call. I can't guess what you want ;-) – Tim Peters Sep 22 '13 at 04:17
  • It worked when I used float() Thank you so much :) – user2803287 Sep 22 '13 at 04:24