-1

I'm trying to get all the inputs added in a different function and then returned to main but I am getting the error: Can't convert 'int' object to str implicitly.

Here's my code:

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()
Blckknght
  • 100,903
  • 11
  • 120
  • 169

2 Answers2

0

On lines 13 and 14, you combine numbers and strings ('\n') with the + operator. + works when both operands are numbers or both are strings, but you cannot mix and match. Convert your numbers to strings with str() before concatenating the pieces.

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • how do i go about doing that? – polskiebmw Dec 06 '16 at 01:40
  • http://www.tutorialscollection.com/python-int-to-string-using-str-method-to-convert-int-into-strings/ – DYZ Dec 06 '16 at 01:43
  • i just got rid of the strings actually. my only problem now is when it goes into the output txt file. instead of adding the numbers it just places them side by side like this "3456789" – polskiebmw Dec 06 '16 at 01:45
  • Please show the parameter that you pass to any of the `file.write()` calls. – DYZ Dec 06 '16 at 01:47
  • may 2016 1500 800 120 110 45 95 550 15008001201104595550 150080012011045955501500800120110459555015008001201104595550150080012011045955501500800120110459555015008001201104595550150080012011045955501500800120110459555015008001201104595550150080012011045955501500800120110459555015008001201104595550 – polskiebmw Dec 06 '16 at 01:49
  • wont let me space it out but instead of adding input3 to input9 it just puts the numbers side by side – polskiebmw Dec 06 '16 at 01:50
  • In your original example, you pass `input1 + "\n" + input2 + "\n" + input3 + "\n" + input4 + "\n"` as the parameter, What do you pass now, after you changed your program? – DYZ Dec 06 '16 at 01:51
  • im talking about file.write(monpay). its not adding them correctly – polskiebmw Dec 06 '16 at 01:54
  • Please update your question and especially the code in it so that we can discuss it. – DYZ Dec 06 '16 at 01:56
  • You do not have any numbers in your code anymore, it's all strings. Your original code was correct, all you had to do was to convert numbers back to strings before concatenating them, as I suggested. You do not seem to understand the difference between strings and numbers. I suggest that you go back to the roginal code and read this answer http://stackoverflow.com/questions/6981495/how-can-i-concatenate-a-string-and-a-number-in-python – DYZ Dec 06 '16 at 02:03
  • so instead should i do like input3 = input("Enter the home mortgage payment amount: ", +str)? – polskiebmw Dec 06 '16 at 02:05
0

Wrap a str( + ) around your input. Then, when you want to use your other functions, change the arguments in the function to be wrapped with int( + )

Will
  • 4,942
  • 2
  • 22
  • 47