Python -简单的兴趣

6rqinv9w  于 2023-04-22  发布在  Python
关注(0)|答案(3)|浏览(74)

我试图为自己写一个代码,这将给予我一个简单的利益的答案,我将使用相同的概念,后来使复利。我有麻烦与我的利率。当我这样做的百分比

r = int(input("rate %: ")

我输入5.4,它不工作,所以我试着在一个十进制的形式,像这样

r = int(input("Rate 0."))

如果我做0.045和0.45,我得到的答案是一样的,那么我该如何解决这个问题呢
下面是我的全部代码

while True:
    while True:
            print('Working out for SIMPLE INTEREST')
            p = int(input("Principl:"))
            r = int(input("Rate 0."))
            t = int(input("Time yrs:"))
            i = p*r
            i = i*t
            a = p + i
            print("Interest = " + str(i))
            print("Accumalated = " + str(a))
            print(str(p) + ' x ' + str(r) + ' x ' + str(t) + ' = ' + str(i) + ' | ' + str(p) + ' + ' + str(i) + ' = ' + str(a))
2jcobegt

2jcobegt1#

int将输入字符串转换为 integer,这是一个 * 整数 *,如45。对于5.4,您需要一个 * 浮点 * 数,您可以使用float函数生成:

r = float(input("rate %: "))

(For专业使用,您甚至可以考虑任意精度的decimal包,但在您的情况下,它可能是多余的。)

anauzrmj

anauzrmj2#

这里是Python程序使用***单一继承***计算简单利息的解决方案。

class SimpleInterest:
    def __init__(self,principle,years,roi):
        self.principle = principle
        self.years = years
        self.roi = roi

class Interest(SimpleInterest):
    def calulate(self):
        si = (self.principle*self.years*self.roi)/100
        return si
    
principle = int(input())
years = int(input())
roi = int(input())

o = Interest(principle,years,roi)
si = o.calulate()
print("Principle amount:",principle)
print("No.Of.Years:",years)
print("Rate of interest:",roi)
print("Simple Interest:",si)
kmb7vmvb

kmb7vmvb3#

这是因为int不支持十进制数
因此,将int(input('something...'))更改为input('sonething...')

相关问题