为什么我的函数在Python中没有循环就一直循环?
下面的代码在第30行之后循环。谁能告诉我我做错了什么?在消息中键入[done]后,它只会再次显示“title>>”提示为什么我的函数在Python中没有循环就一直循环?,python,function,loops,while-loop,Python,Function,Loops,While Loop,下面的代码在第30行之后循环。谁能告诉我我做错了什么?在消息中键入[done]后,它只会再次显示“title>>”提示 from lxml import etree import time import datetime def memory(): ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') post = open('post.txt',
from lxml import etree
import time
import datetime
def memory():
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
post = open('post.txt', 'w')
document = etree.Element('document')
title = raw_input('title>>')
while 1:
message = raw_input('post>>')
post.write(message + '\n')
if '[done]' in message:
post.close()
break
post = open('post.txt', 'r')
postf = post.read()
article = etree.SubElement(document, 'article', title=title, date=st)
article.text = postf
post.close()
with open('postf.txt', 'a') as file:
file.write(etree.tostring(document, pretty_print=True) + '\n')
file.close()
return(document, article)
memory()
document = memory()
article = memory()
print etree.tostring(document, pretty_print=True)
print '<<Your Message>>'
print article.text
从lxml导入etree
导入时间
导入日期时间
def memory():
ts=时间。时间()
st=datetime.datetime.fromtimestamp(ts).strftime(“%Y-%m-%d%H:%m:%S”)
post=open('post.txt','w')
document=etree.Element('document')
title=原始输入(“title>>”)
而1:
消息=原始输入(“post>>”)
post.write(消息+“\n”)
如果消息中有“[完成]”:
post.close()
打破
post=open('post.txt','r')
postf=post.read()
article=etree.SubElement(文档“article”,title=title,date=st)
article.text=postf
post.close()
打开('postf.txt','a')作为文件:
file.write(etree.tostring(document,pretty\u print=True)+'\n')
file.close()文件
退货(文件、物品)
内存()
文档=内存()
article=内存()
打印etree.tostring(文档,pretty\u print=True)
打印“
打印文章.text
它应该循环“post>>”提示,直到看到[done],然后它应该中断循环并完成函数。我觉得这很简单,但在过去的一个半小时里,我一直在改变一些事情,但都无济于事
提前感谢。您是否意识到您正在调用
memory()
三次,因此您应该至少看到title
三次?…嗯。我忘了括号里叫它。。。我是哑巴。谢谢,不用担心。我没有把它作为一个答案来写,因为我不确定这是疏忽还是你在3次之后反复出现了title
。