写一个程序,提示访客输入名字,并给出一个招呼语,然后将访客名字写入guest.txt文件中作为备份
print("Enter 'quit' when you are finished\n")
filename = 'guest.txt'
while True:
name = input('Please enter your name>>>')
if name == 'quit':
break
else:
with open(filename,'a') as file:
file.write(name.title()+'\n')#将访客写入的名字改为首字母大写
print('Welcome,'+name.title())
dicision = input('Do you want to continue? y/n>>>')
if dicision == 'n':
break
输入quit可以结束,或者输入n也可以结束
运行如:
Enter 'quit' when you are finished
Please enter your name>>>heiban
Welcome,Heiban
Do you want to continue? y/n>>>y
Please enter your name>>>xiangpica
Welcome,Xiangpica
Do you want to continue? y/n>>>y
Please enter your name>>>fenbi
Welcome,Fenbi
Do you want to continue? y/n>>>n
>>>
guest.txt文件为:
Heiban
Xiangpica
Fenbi