Python代码正在跳过其他内容

时间:2016-03-23 21:37:51

标签: python if-statement for-loop skip

嗨所以这是我的代码,因为某种原因在最后一个for循环之后,代码只是直接跳到else语句并打印了大约4次然后转到if语句并打印解决方案。请有人帮忙解决这个问题。

while True:
   d = {}
   with open("keyword_database.txt") as f:
       for line in f:
          (key,val) = line.split(":")
          d[str(key)] = val
   try:
      userinput=input(str("What is the problem with your phone?\nEnter here in lower:"))
      print()
   except:
      print ("Invalid Input")
   for word in userinput.split(): 
           if word in d:
              print(d[word])
              print()
           else:
              print("Please Re-Phrase your problem and Try Again")
              print()

这是我运行代码后打印的内容。

  

手机有什么问题?在这里输入:我的手机是   破

     

请重新发表您的问题,然后重试

     

请重新发表您的问题,然后重试

     

请重新发表您的问题,然后重试

     

问题=破解解决方案=将手机送到维修店并获取   它固定在那里,但请记住,如果有人打开你的   除了苹果之外的电话,保修将是无效的(非活动)。

1 个答案:

答案 0 :(得分:0)

试试这个:

for word in userinput.split(): 
    result = ""
    if word in d:
        result += d[word] + "\n"

if (len(result) == 0):
    result += "Please Re-Phrase your problem and Try Again\n"
print (result)