通过划分答案,遇到python问题

时间:2016-12-14 22:23:20

标签: python python-2.7

这几乎是完美的,只是计算得出错误的结果,这是因为它将答案除以100 我必须将利率除以100.因此,用户输入的r值需要除以100

import math

p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n")/100)
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 (A)
else:
    print(B)

2 个答案:

答案 0 :(得分:0)

所以,你只需要改变这一行:

r = int(raw_input("Please input interest rate: \n")/100)

要:

r = int(raw_input("Please input interest rate: \n"))/100

raw_input("Please input interest rate: \n")/100表示您将结果转换为int将字符串除以100。相反,您需要将用户输入转换为int并将其除以100。

注意:在Python 2中,10/100例如返回0,因此您需要除100.0而不是100

>>> 10/100
0
>>> 10/100
0
>>> 10/100.0
0.1

答案 1 :(得分:0)

像ettanany所说,主要问题是利率投入线。你的第9行也有一个^而不是**。我最后在if语句中添加了一点,以确保只有当用户输入单词“compound”时才打印出复合兴趣的结果。否则任何事情都会导致不必要的选择。

import math

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

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

if interest == "simple":
    print (A)
elif interest == "compound":
    print(B)
else:
    print("Error on the interest type")