-3

I am trying to get a float value but it keeps on giving me a decimal value as an answer.

import math

p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n"))
t = int(raw_input("Please insert number of years of the investment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")

A = p*(1+r*t) 
B = p*(1+r)^t 

if interest == "simple":
    print (float(A/100))
else:
    print(float(B/100))
CDspace
  • 2,639
  • 18
  • 30
  • 36
Duvall912
  • 63
  • 6
  • if you use Python 2 and `A` is integer then `A/100` gives integer. You need `A/100.0` or `float(A)/100` – furas Dec 07 '16 at 21:44
  • 1
    Aside from the integral division issue, you are using the wrong exponentiation operator. it is `**` in Python. – hilberts_drinking_problem Dec 07 '16 at 21:44
  • @shash678: OP didn't even bother to correct the mistakes that were pointed out in that previous question ... :/ (And, so I see, [the one before that](http://stackoverflow.com/q/40979415/2564301)...) – Jongware Dec 07 '16 at 21:45
  • I'm suppose to convert the interest and not the answer. I tried putting it by the variable then it returned the following exception. @RadLexus r = int(raw_input("Please input interest rate: \n")/100) Traceback (most recent call last): File "", line 4, in TypeError: unsupported operand type(s) for /: 'str' and 'int' – Duvall912 Dec 08 '16 at 15:06

2 Answers2

4

float(A/100) first calculates A/100, which are both ints, so the result is an int, and only then converts to float. Instead you could use:

float(A)/100

or:

A/100.
kmaork
  • 5,722
  • 2
  • 23
  • 40
0

Here is the problem. In python2 the division between integers gives another integer (this is not true in python3).

42 / 100 # return 0 

The solution is to keep one to float.

42 / 100.0 #return 0.42
Lucas
  • 6,869
  • 5
  • 29
  • 44