You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python运行复利计算器报TypeError: 不支持tuple与int的/运算,请问哪里出错了?

你代码的错误出在输入变量的定义环节。
你错误地将类型标记和input返回值用逗号放在圆括号内,导致P、R、m、t四个变量实际是包含类型对象和输入字符串的元组,而非可参与计算的数值类型,因此后续做除法运算时触发了元组和整数无法运算的报错。

正确的类型转换写法是用类型函数直接包裹input(),把用户输入的字符串转为对应数值类型,修正后的完整代码如下:

# 输入
P = float(input("Enter the starting principal: "))
R = float(input("Enter the annual interest rate: "))
m = int(input("How many times per year is the interest compounded? "))
t = float(input("For how many years will the account earn interest? "))

# 计算
FV = P * ( 1 + ( (R/100) / m ) ) ** ( m * t)

# 输出
print("At the end of ", t, "years. You will have $", format(FV, ",.2f") ) 

修改后重新运行代码即可正常执行复利计算。

内容的提问来源于stack exchange,提问作者meg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 16:57:02