【问题标题】:final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int'final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int'
【发布时间】:2016-04-29 04:27:12
【问题描述】:

我是 Python 新手(运行 Python 3),已经搜索了所有地方,但仍然坚持这一点。 我知道我有一个字符串,但需要将其转换为整数作为最终答案。下面是我输入的代码来测试它。我被困在我得到的错误上。 这是一个复利计算器,用户必须输入本金、年利率、复利频率和年份。 我做了一些研究,发现我可以将 ('inf') 用​​于 p、n 和 r 作为声明,表示可以是大数或小数。基本上用户可以输入任何数字。

P = 10000
n = 12
r = 0.08

p = int(input("Enter starting principle please. "))
n = int(input("Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) "))
r = input("Enter annual interest amount. (decimal) ")
t = int(input("Enter the amount of years. "))

final = P * (((1 + (r/n)) ** (n*t)))

print ("The final amount after", p, "years is", final)

错误:

Enter starting principle please. 75000
Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) 12
Enter annual interest amount. (decimal) 0.03
Enter the amount of years. 15
Traceback (most recent call last):
  File "C:\Users\Joshua\AppData\Local\Programs\Python\Python35-32\Program Five.py", line 10, in <module>
    final = P * (((1 + (r/n)) ** (n*t)))
TypeError: unsupported operand type(s) for /: 'str' and 'int'

这是我第一次使用这个网站,如果我遗漏了任何信息,请告诉我。我愿意接受任何可以帮助我解决此问题的建议。

                             Edit

抱歉让您久等了。好的,我相信我可以通过 @Rafael Cardoso 的 Stop and Think 建议正常工作。谢谢你。

P = 10000
n = 12
r = 0.08

p = int(input("Enter starting principle please. "))
n = int(input("Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) "))
r = float(input("Enter annual interest amount. (decimal) "))
t = int(input("Enter the amount of years. "))

final = P * (((1 + (r/n)) ** (n*t)))

print ("The final amount after", p, "years is", final)

当我运行它时,不会弹出任何错误。稍事休息后,我发现('inf') 导致了这个问题,如果我把 p,n,r 留在那里,它可以正常工作。

再次感谢大家!

【问题讨论】:

  • (r/n) where r ist str and n is int 应该清除它
  • 如果我将 ('inf') 用​​于 P、n 和 r,然后删除所有“int”我最终得到 TypeError: unsupported operand type(s) for /: 'str'和'str'

标签: python string int typeerror


【解决方案1】:

根据堆栈跟踪,r 是一个str。您应该只使用数字,因此为了进行操作,请将 r 转换为 float(因为它处理十进制数字)

r = float(input("Enter annual interest amount. (decimal) "))

编辑

好的,让我们停下来想一想。当您使用 python 3 时,输入函数会将您输入的值存储为字符串(即文本)。你希望它们是数字。

因此,您必须将所有内容都转换为数字,以便能够进行乘法、除法等操作。例如,"a"/"b" 是什么意思?那么"a1"/"b2" 呢?甚至"1"/"2"?他们没有任何意义。但是,1/2 是有道理的。不同之处在于我们在前 3 个示例中处理的是文本,而在最后一个示例中处理的是数字。

要使其正常工作,您应该将所有内容转换为intfloat。这些types 是数字,您可以放心地用它们进行数学运算。

【讨论】:

  • 我已经尝试了你的两个答案@Rafael Cardoso @WorldSEnder。我要么得到打印的语法错误,要么得到 r = ........ 的语法错误,当我使用导入小数时,它会说导入是语法错误。我觉得我的蟒蛇在星期五跑得不开心。
  • @user280832 我确定你的错误是非常基本的。在此处发布此语法错误
  • @user280832,因为您将字符串(“之后的最终金额”)与整数 p 连接:不可能。做类似print ("The final amount after %s years is %s.' % (p, final))
  • Enter starting principle please. 75000 Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) 12 Enter annual interest amount. (decimal) 0.03 Enter the amount of years. 15 Traceback (most recent call last): File "C:\Users\Joshua\AppData\Local\Programs\Python\Python35-32\Program Five.py", line 10, in &lt;module&gt; final = P * (((1 + (r/n)) ** (n*t))) TypeError: can't multiply sequence by non-int of type 'float'
  • 发现语法错误,这是我可怕的拼写。让脚本运行,但出现上述错误。
【解决方案2】:

您可以使用模块decimal:

import decimal
r = decimal.Decimal(input("Enter annual interest amount. (decimal) "))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-04
    • 2021-03-27
    • 2020-04-14
    • 1970-01-01
    • 2023-04-02
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多