0

I've made a program for my GCSE Coursework and need some help regarding saving all of the users inputs to a text file in notepad can any help would be greatly appreciated

the code is

print("Use Yes Or No For Answers That Require It") 
from datetime import datetime #imports time package count=0 #sets count to 0 
    while True: 
        count +=1 
        if count >3: 
            break 
        name=input("what is your name\n") 
        yob=int(input("what is your yob\n")) 
        year = datetime.now().year #sets year 
        age=year-yob # sets age as a constant 
        print("so you are",age,) 
        user_input=input ("is this correct\n") 
        if user_input==("yes"): 
            print("nice") 
        else: 
            print("oops, you must be",age-1)
matt
  • 10,892
  • 3
  • 22
  • 34
BENNAY
  • 1
  • Have you tried anything? Like `inputFile = open("inputs.txt", 'w')` and then writing to it? Are you using a library? How are you getting user input? – matt Jul 05 '17 at 09:31
  • print("Use Yes Or No For Answers That Require It") from datetime import datetime #imports time package count=0 #sets count to 0 while True: count +=1 if count >3: break name=input("what is your name\n") yob=int(input("what is your yob\n")) year = datetime.now().year #sets year age=year-yob # sets age as a constant print("so you are",age,) user_input=input ("is this correct\n") if user_input==("yes"): print("nice") else: print("oops, you must be",age-1) – BENNAY Jul 05 '17 at 09:41
  • that's the code for the program I cant figure out how to save to text file – BENNAY Jul 05 '17 at 09:42
  • could you edit the code and send back the text file is named on my computer as data_file – BENNAY Jul 05 '17 at 09:42
  • I'm quite new to Python programming as you can tell – BENNAY Jul 05 '17 at 09:44
  • Can you edit the question and add your code example, with formatted code. – matt Jul 05 '17 at 10:19
  • I cant do the formatted code as the school only has python in the IT rooms – BENNAY Jul 05 '17 at 12:38
  • I formatted it for you, your excuse is lame though. – matt Jul 05 '17 at 13:04

1 Answers1

0

For the example you have pasted, I would add the write to the statement where you confirm if everything is correct.

    if user_input==("yes"): 
        print("nice")
        with open('file.txt', 'a') as outfile:
            outfile.write("%s\t%d\t%d\n"%(name, yob, year))
    else: 
        print("oops, you must be",age-1)

Although, you could put it anywhere. Check this answer for a bit more information about writing files in python,

matt
  • 10,892
  • 3
  • 22
  • 34