Python 结合输入和数学

Python 结合输入和数学,python,Python,我正在尝试使用输入构建一个未来值计算器,但在数学方面遇到了麻烦。有人能提出解决方案吗?另外,在计算之前,如何将响应转换为整数或浮点 present= input("What is the present value?") rate = input("What is the rate of return?") periods = input("What is the number of periods?") answer = {prese

我正在尝试使用输入构建一个未来值计算器,但在数学方面遇到了麻烦。有人能提出解决方案吗?另外,在计算之前,如何将响应转换为整数或浮点

present= input("What is the present value?")
rate = input("What is the rate of return?")
periods = input("What is the number of periods?")
answer = {present} * (1 + {rate} ** {periods})
print(f"The  future value is", int(answer))

只要在它们周围加上浮点数(十进制数)


只要在它们周围加上浮点数(十进制数)

  • 您可以使用
    int()
    将输入转换为整数

    present= int(input("What is the present value?"))
     rate =float( input("What is the rate of return?"))
     periods = int( input("What is the number of periods?"))
    
     answer = present * (1 + rate ** periods)
    
     print(f"The  future value is", int(answer))
    
      • 您可以使用
        int()
        将输入转换为整数

        present= int(input("What is the present value?"))
         rate =float( input("What is the rate of return?"))
         periods = int( input("What is the number of periods?"))
        
         answer = present * (1 + rate ** periods)
        
         print(f"The  future value is", int(answer))
        

      在对输入进行数学运算之前,您需要将输入转换为数字(或
      float
      int
      )。当您进行数学运算时,不需要大括号语法(在该上下文中,它被解释为set文本,这不是您在这里想要的),但它在f-strings中用于将变量格式化为字符串

      present= float(input("What is the present value?"))
      rate = float(input("What is the rate of return?"))
      periods = int(input("What is the number of periods?"))
      answer = present * (1 + rate) ** periods
      print(f"The  future value is {answer}")
      

      (编辑)此外,我认为您希望将
      (1+比率)
      提高到
      时段的幂次
      ,而不是像使用括号那样。

      在对输入进行数学运算之前,您需要将输入转换为数字(或
      浮点
      整数
      )。当您进行数学运算时,不需要大括号语法(在该上下文中,它被解释为set文本,这不是您在这里想要的),但它在f-strings中用于将变量格式化为字符串

      present= float(input("What is the present value?"))
      rate = float(input("What is the rate of return?"))
      periods = int(input("What is the number of periods?"))
      answer = present * (1 + rate) ** periods
      print(f"The  future value is {answer}")
      

      (编辑)此外,我认为您希望将
      (1+费率)
      提高到
      时段的幂次方
      ,而不是使用括号。

      首先要注意的是,您的未来值公式不正确。它应该是pv*(1+r)^n,而不是pv*(1+r^n)。这是假设您的利率(r)对应于每个期间的利率

      下面是一些快速代码,可以实现您可能正在寻找的功能:

      pv = float(input("what is the pv? "))
      r = float(input("what is the rate? "))
      n = float(input("how many periods? "))
      
      print("The future value is {}".format(pv*(1+r)**n))
      
      输出:

      what is the pv? 1
      what is the rate? 0.05
      how many periods? 10
      The future value is 1.628894626777442
      

      首先要注意的是,您的未来值公式不正确。它应该是pv*(1+r)^n,而不是pv*(1+r^n)。这是假设您的利率(r)对应于每个期间的利率

      下面是一些快速代码,可以实现您可能正在寻找的功能:

      pv = float(input("what is the pv? "))
      r = float(input("what is the rate? "))
      n = float(input("how many periods? "))
      
      print("The future value is {}".format(pv*(1+r)**n))
      
      输出:

      what is the pv? 1
      what is the rate? 0.05
      how many periods? 10
      The future value is 1.628894626777442
      

      这是行不通的,因为速率中可能有一个小数,而不是整数。如果在小数中提到速率,那么我们可以将输入转换为浮点而不是整数。这行不通,既然速率中可能有一个小数,而不是整数,如果速率是用小数表示的,那么我们可以将输入转换为浮点而不是整数。为什么您要问转换是如何工作的:您已经为代码最后一行中的
      应答
      执行了转换?至于其余的:请展示您解决问题的尝试,并指出您遇到问题的地方。是的,尽管我试图包含int,但上面的最后一行代码不起作用,我认为这就是您所说的“强制转换”。我想我缺少的关键是,我需要首先通过float或int函数将输入转换为数字。为什么你要问转换是如何工作的:你已经为代码最后一行的
      answer
      这样做了?至于其余的:请展示您解决问题的尝试,并指出您遇到问题的地方。是的,尽管我试图包含int,但上面的最后一行代码不起作用,我认为这就是您所说的“强制转换”。我想我缺少的关键是我需要首先通过float或int函数将输入转换成数字。