【问题标题】:Saving user output to file with while loop使用 while 循环将用户输出保存到文件
【发布时间】:2020-10-25 15:24:03
【问题描述】:

我正在做一些练习,但我无法弄清楚这一点。

编写一个提示用户输入姓名的 while 循环。当他们输入他们的名字时,在屏幕上打印一个问候语,并在一个名为 guest_book.txt 的文件中添加一行记录他们的访问。确保每个条目出现在文件的新行中。

老实说,我在这方面花了很多时间,似乎我不了解处理文件的逻辑。我认为f.write 应该直接在with open 下,以便将用户input 保存在文件中,但这就是我所知道的。你能帮帮我吗?

我的尝试:

filename = 'guest_book.txt'

with open(filename, 'w') as f:
    lines = input('Input your name to save in the file: ')
    while True:
        for line in lines:
            f.write(input('Input your name to save in the file'))

我对第二个抱有更多希望,但仍然没有成功。

filename = 'guest_book.txt'
prompt = "Input your name to be saved in guest book: "
active = True

with open(filename, 'w') as f:
    while active:
        message = f.write(input(prompt))

        if message == 'quit':
            break
        else:
            print(message)

【问题讨论】:

    标签: python loops file


    【解决方案1】:

    在您编写代码时,稍作调整即可使代码正常工作。主要问题是您不能使用f.write() 的输出作为消息的值,因为它不包含消息,它包含写入文件的字符数,所以它永远不会包含quit .此外,您要在检查了quit 之后执行write,否则您会将quit 写入文件。

    filename = 'guest_book.txt'
    prompt = "Input your name to be saved in guest book: "
    active = True
    
    with open(filename, 'w') as f:
        while active:
            message = input(prompt)
    
            if message == 'quit':
                active = False
            else:
                f.write(message + '\n')
                print(message)
    

    注意:我已将 break 更改为 active = False,因为您将其写为 while active:,但您只需要 while True:break 就可以了

    【讨论】:

    • 非常感谢您的帖子!我明白你为什么要做出这些调整。他们都说得通。但是,我已将其复制到 pycharm 中,但它仍然没有将任何内容保存到文件中。知道可以做什么吗?
    • 我已经在 PyCharm 中运行了它,它工作正常,我在文件中得到了输出。你退出循环关闭文件了吗?
    • OOOOOOOOOOOO,我需要退出才能保存它。我不知道那个sry。现在它可以工作了,我假设它会在我输入每个名称时保存它。
    【解决方案2】:
    filename = 'guest_book.txt'
    prompt = "Input your name to be saved in guest book: "
    active = True
    
    with open(filename, 'w') as f:
        while active:
            message = input(prompt)
    
            if message == 'quit':
                break
            else:
                print(message)
            
            f.write(message + '\n')
    

    这可能有效。分配message = f.write(...) 会使它的值成为f.write() 的返回值。

    【讨论】:

    • 也正如@yagil 所说,如果文件已经存在,您应该使用open(filename, 'a') 将其附加到文件中。
    【解决方案3】:

    试试这个..

    filename = 'guest_book.txt'
    name = ''
    
    with open(filename, 'w') as f:
        while name != 'quit':
            name = input('Input your name to save in the file: ')
            if(name == 'quit'):
                break
            f.write(name + '\n')
    

    【讨论】:

      【解决方案4】:

      当 a 代表追加时,您可以使用 open(filename, 'a'),这样您可以在每次循环迭代时将新行追加到文件中。

      见:How do you append to a file in Python?

      要了解文件处理,请参阅:https://www.w3schools.com/python/python_file_handling.asp

      祝你好运!

      【讨论】:

        【解决方案5】:

        对于任何想知道如何解决整个问题的人。我们使用 append 而不是 write 是为了保留过去一直使用该程序的所有客人。写入会覆盖文件。

        filename = 'guest_book.txt'
        
        print("Enter 'quit' when you are finished.")
        while True:
            name = input("\nWhat's your name? ")
            if name == 'quit':
                break
            else:
                with open(filename, 'a') as f:
                    f.write(name + "\n")
                print(f"Hi {name}, you've been added to the guest book.")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-13
          • 2022-11-28
          • 1970-01-01
          • 1970-01-01
          • 2017-06-18
          • 2014-04-12
          • 2019-05-19
          • 1970-01-01
          相关资源
          最近更新 更多