【问题标题】:How Do I Save All User Inputs To A Notepad text File In Python 3.5如何在 Python 3.5 中将所有用户输入保存到记事本文本文件中
【发布时间】:2017-07-05 21:08:52
【问题描述】:

我已经为我的 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)

【问题讨论】:

  • 你有没有尝试过?喜欢inputFile = open("inputs.txt", 'w') 然后写信给它?你在使用图书馆吗?您如何获得用户输入?
  • print("对需要它的答案使用是或否") 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 # 设置年龄作为常量 print("so you are",age,) user_input=input ("is this correct\n") if user_input==("yes"): print("nice") else: print("oops, you必须是",age-1)
  • 这是程序的代码,我不知道如何保存到文本文件
  • 你能编辑一下代码,然后发回我电脑上的文本文件名为 data_file
  • 如您所知,我对 Python 编程很陌生

标签: python-3.5


【解决方案1】:

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

    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 中写入文件的信息,

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2022-10-15
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多