2

I'm going through some exercises and I can't just figure this out.

Write a while loop that prompts users for their name. When they enter their name, print a greeting to the screen and add a line recording their visit in a file called guest_book.txt. Make sure each entry appears on a new line in the file.

Honestly, I spent way to much time on that and it seems like I'm not understanding the logic of working with files. I think the f.write should be directly under with open in order for the user input to be saved in the file but that's as much as I know. Can you please help me?

My attempts:

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'))

I had some more hopes for the second one but still doesn't work.

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)
David Buck
  • 3,752
  • 35
  • 31
  • 35

5 Answers5

4

A bit of rejigging will get the code as you've written it to work. The main issue is that you can't use the output of f.write() as the value of message, as it doesn't contain the message, it contains the number of characters written to the file, so it will never contain quit. Also, you want to do the write after you have checked for quit otherwise you will write quit to 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 = input(prompt)

        if message == 'quit':
            active = False
        else:
            f.write(message + '\n')
            print(message)

Note: I have changed the break to active = False as you have written it as while active:, but you would have been fine with just while True: and break

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • thank you very much for your post! I see why you've made those adjustments. they all make sense. However, I've copied this to pycharm and it still doesn't save anything to the file. Any idea what can be done? – Michal Patyjewicz Jul 05 '20 at 11:55
  • 1
    I've run it in PyCharm and it works fine and I get output in the file. Have you quit the loop to close the file? – David Buck Jul 05 '20 at 11:57
  • OOOOOooooooo, I need to quit to save it. I didn't know that sry. Now it works, I assumed it will save it as I input every name. – Michal Patyjewicz Jul 05 '20 at 12:01
2

You can use open(filename, 'a') when a stands for append that way you can append a new line to the file each loop iteration.

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

To learn about file handling see: https://www.w3schools.com/python/python_file_handling.asp

Good luck!

yagil
  • 55
  • 1
  • 7
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')

This might work. Assigning message = f.write(...) will make its value the return value of f.write().

mmfallacy
  • 186
  • 8
  • 2
    and also as @yagil stated, you should use `open(filename, 'a')` to append it on the file, if it already exists. – mmfallacy Jul 05 '20 at 11:53
2

Try this..

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')
Parth Choksi
  • 166
  • 8
1

For anyone wondering how to solve the whole thing. We're using append instead of write in order to keep all the guests that have been using the programme in the past. Write would override the file.

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.")