如何将所有用户输入保存到记事本文本文件在Python 3.5中

时间:2017-07-05 09:28:52

标签: python-3.5

我已经为我的GCSE课程制作了一个程序,需要一些帮助,将所有用户输入保存到记事本中的文本文件中,我们将非常感谢任何帮助

代码是

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)

1 个答案:

答案 0 :(得分:0)

对于您粘贴的示例,我会将写入添加到您确认一切是否正确的语句中。

    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)

虽然,你可以把它放在任何地方。检查this answer以获取有关在python中编写文件的更多信息,

相关问题