无法转换' int'对象为str隐式模块化python

时间:2016-12-06 01:32:42

标签: python

我试图将所有输入添加到其他函数中然后返回到main但我收到错误:def main(): input1 = input("Enter the month: ") input2 = input("Enter the year: ") input3 = input("Enter the home mortgage payment amount: ") input4 = input("Enter the car payment amount: ") input5 = input("Enter the gas payment amount: ") input6 = input("Enter the electric payment amount: ") input7 = input("Enter the phone payment amount: ") input8 = input("Enter the cell payment maount: ") input9 = input("Enter the miscellaneous payment amount: ") monpay = calcMonthlyPay(input3,input4,input5,input6,input7,input8,input9) yearpay = calcYearlyPay(monpay) print("SUCCESS: Data wrtten to the may.txt file") file = open("may.txt", "w") file.write(input1 + "\n" + input2 + "\n" + input3 + "\n" + input4 + "\n") file.write(input5 + "\n" + input6 + "\n" + input7 + "\n" + input8 + "\n" + input9 + "\n") file.write(monpay + "\n") file.write(yearpay) file.close() def calcMonthlyPay(input3,input4,input5,input6,input7,input8,input9): monthpay = (input3+input4+input5+input6+input7+input8+input9) return monthpay def calcYearlyPay(monpay): yearpay = monpay * 12 return yearpay main()

这是我的代码:

{{1}}

2 个答案:

答案 0 :(得分:0)

在第13和14行,您将数字和字符串('\n')与+运算符组合在一起。当两个操作数都是数字或两者都是字符串时,+有效,但你不能混合和匹配。在连接各个部分之前,将您的数字转换为str()的字符串。

答案 1 :(得分:0)

围绕您的输入换str( + )。然后,当您想要使用其他函数时,更改函数中的参数以包裹int( + )