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
相关产品推荐
相关产品推荐

