有没有办法将十进制输入到预期的整数字符串中

问题描述 投票:-1回答:2

使用我的基本兴趣计算器,我不断收到错误

 r = int(raw_input("What is the rate of growth?\n=> "))
 ValueError: invalid literal for int() with base 10: '.52'

我使用的数字是20,000的本金率52%(.052是我投入的)复合是每半年有3个期间

    import math

    p = int(raw_input("what is the principal value?\n=> "))
    r = int(raw_input("What is the rate of growth?\n=> "))
    c = int(raw_input("How many compoundings per periods are taking place? 
    \n=> "))
    t = int(raw_input("How mant periods are taking place?\n=>  "))

    number = str(p*(1+r/c)**c*t)
    print(number)

编辑:抱歉蹩脚的变量,这就是我的作业;)

python python-2.7
2个回答
2
投票

如果你想要浮点数,为什么要将输入字符串转换为int

更改

r = int(raw_input("What is the rate of growth?\n=> "))

r = float(raw_input("What is the rate of growth?\n=> "))

3
投票

没有;这是一个矛盾:int没有小数点。 .052不是int;这是一个浮动。使用正确的转换:

rate = float(raw_input("What is the rate of growth?\n=> "))

另外,使用有意义的变量名称;单字母变量往往会联合起来并在以后咬你。

© www.soinside.com 2019 - 2024. All rights reserved.