简体   繁体   English

Python代码跳到其他

[英]Python code is skipping to else

Hi so this is my code an for some reason after the last for loop the code just skips straight to the else statement and prints it about 4 times and then goes to the if statement and prints the solution. 嗨,这是我的代码,由于最后一个for循环后的某种原因,该代码只是直接跳到else语句并打印大约4次,然后转到if语句并打印解决方案。 Please can someone help with this. 请有人帮忙。

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()

And this is what it prints once I run the code. 这是我运行代码后输出的内容。

What is the problem with your phone? 您的手机有什么问题? Enter here in lower: my phone is broken 在下方输入此处:我的手机坏了

Please Re-Phrase your problem and Try Again 请重新输入您的问题,然后重试

Please Re-Phrase your problem and Try Again 请重新输入您的问题,然后重试

Please Re-Phrase your problem and Try Again 请重新输入您的问题,然后重试

Problem = Broken Solution = Take your phone to the repair shop and get it fixed from there however do keep in mind if someone else opens your phone apart from apple then the warrenty will be VOID (NOT ACTIVE). 问题=解决方案破裂=将您的手机带到维修店并从那里修理好,但是请记住,如果有人打开了除苹果之外的其他电话,则保修将失效(无效)。

try this: 尝试这个:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM