Python Forum

Full Version: Writing Answers From Questionnaire to a new .txt Document
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello I am trying to make a questionnaire, and I am trying to get the answers to be written into a .txt document. It was working until I added more code now I get an error like this:

Error:
line 32, in main fout.write(finalOut[i] + "\n") TypeError: unsupported operand type(s) for +: 'int' and 'str'
Please help me!

Here is my code:

def main():
    fout = open("Questionnaire.txt",'w')
    input1 = input("Hello!\n" "What is your name\n")
    print ("Cool name")
    print (input1)
    print ("Welcome to my questionnare")
    print ("There is four questions, answer them truthfully please")
    print ("Question1")
    while True:
        try:
            Question1 = int(input("What is your favourite number?\n"))
        except ValueError:
            print("That's not a number.")
            continue
        else:
            break
    print ("Nice")
    print ("Next Question")
    Question2 = input("What is your favourite colour?\n")
    print ("cool")
    print ("Next Question")
    Question3 = (input("What is your favourtie ice cream flavour\n"))
    if Question3 in  ['Vannila','vannila']:
        print ("That is my favourite flavour too")
    else :
        print ("That is a cool flavour")
    Question4 = input("What is your favourite food?\n")
    print ("Great")
    print ("Thanks for doing my questionnaire, all your answers will be found in the text document.")
    finalOut = ["Here are your answers",input1,Question1,Question2,Question3,Question4]
    for i in range(0, len(finalOut)):
        fout.write(finalOut[i] + "\n")
if __name__ == '__main__':
    main()
Change line 32 to
fout.write(str(finalOut[i]) + "\n")
You are trying to concatenate a string with an integer which is not going to happen.
Thank you for fixing my problem!
or use:
fout.write('{}\n'.format(finalOut[i]))