Reputation: 65
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)
Upvotes: 2
Views: 1502
Reputation: 65
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.")
Upvotes: 1
Reputation: 3842
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
Upvotes: 4
Reputation: 83
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!
Upvotes: 2
Reputation: 186
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')
Upvotes: 2
Reputation: 186
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().
Upvotes: 2