【发布时间】:2014-07-13 12:11:08
【问题描述】:
我被这段代码卡住了。 所以,我正在制作一个 IRC 机器人来回答人们的问题。当有人提出问题时,我将其写入 .txt 文件;
main.py
Import os
Import hi
Question_file = 'question.txt'
Answer_file = 'answer.txt'
while True:
Question = raw_input ("Question?").lower()
with open(Question_file, "w") as q:
q.write(Question.lower())
q.close()
with open(Answer_file, "r") as m:
answer = m.read()
if 'hi' in Question:
print ("hi")
else:
print answer
现在一切都好。问题在于 hi.py
嗨.py
Import os
Import string
Question_file = 'question.txt'
Answer_file = 'answer.txt'
with open (Question_file, "r") as f:
question = f.read()
if "what year is it" in question:
with open(Answer_file, "w") as r:
r.write("2014")
if "what month is it" in question:
with open(Answer_file, "w") as r:
r.write("July")
问题是,Hi.Py 不会写出正确答案?它只写第一个问题的答案,并在每次提出问题时打印答案?
【问题讨论】:
-
那些脚本甚至不应该运行 - Python 区分大小写。你的缩进搞砸了。还有其他几个问题,但除非您提供Minimal, Complete, Tested and Readable example,否则我认为回答这个问题没有任何意义。
-
我知道我的压痕可能不是最好的,这就是我来寻求帮助的原因。
-
“不是最好的”不是这里的问题。由于大量的语法错误和一个(明显的)缩进错误,您发布的脚本甚至无法运行。运行这些脚本时,您不会得到您所描述的行为。
-
让我快速测试一下。对此感到抱歉。
-
另外,你为什么要标记 Python 3.x 和 Python-idle 这个问题?
标签: python python-2.7 python-idle python-import