Python Forum
Writing Answers From Questionnaire to a new .txt Document
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing Answers From Questionnaire to a new .txt Document
#1
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()
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thank you for fixing my problem!
Reply
#4
or use:
fout.write('{}\n'.format(finalOut[i]))
Reply
#5
also check https://python-forum.io/Thread-Basic-Nev...n-sequence
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'answers 2' is not defined on line 27 0814uu 4 671 Sep-02-2023, 11:02 PM
Last Post: 0814uu
Bug Writing in a document problem IOHANNES 4 1,019 Oct-26-2022, 08:58 PM
Last Post: IOHANNES
  Division calcuation with answers to 1decimal place. sik 3 2,093 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Cannot Assign right Answers To Shuffled Questions Boblows 6 2,685 Jan-22-2021, 09:41 AM
Last Post: buran
  Using answers from an input function Totalartist 3 2,143 Jul-19-2019, 02:02 PM
Last Post: snippsat
  Need Answers 20nick20 6 5,351 Apr-29-2017, 04:06 PM
Last Post: Larz60+
  Code that generates MD5 hashes from IPv6 addresses giving differant answers? PyMD5 4 6,383 Oct-17-2016, 02:39 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020