无法将“int”对象隐式转换为str模块化python

2024-06-16 16:04:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将所有输入添加到另一个函数中,然后返回到main,但得到了错误:Can't convert 'int' object to str implicitly。你知道吗

这是我的密码:

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()

Tags: theinputpaymentamountfileenterinput3input4
2条回答

在第13行和第14行,将数字和字符串('\n')与+运算符组合。+在两个操作数都是数字或都是字符串时工作,但不能混合和匹配。在连接片段之前,用str()将数字转换为字符串。你知道吗

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

相关问题 更多 >