编写一个while循环,提示用户输入其名字。用户输入其名字后,在屏幕上打印一句问候语,并将一条访问记录添加到文件guest_book.txt中。确保这个文件中的每条记录都独占一行。
用Python2.7
#!/usr/bin/env python
# -*- coding: utf-8 -*-
filename='guest_book.txt'
while True:
your_name=raw_input('Please write down your name:')
if your_name=='q':
break
else:
print('Hello, '+your_name+' !')
with open(filename,'a') as file_object:
file_object.write(your_name+" has gone here.\n")
结果: