我必须为故障排除系统编写代码,该系统使用用户输入中的关键字从文本文件中提供解决方案。我有一个If语句列表,它指示程序从一个织物中读取特定的行,并根据用户输入中的特定关键字将它们作为解决方案呈现。你知道吗
但是,无论用户输入什么,程序似乎都会显示文本文件中的前两行(程序似乎卡在第一个if语句上)。你知道吗
Keywords = ['screen', 'display', 'audio', 'sound',]
Keywords2 = ['cracked', 'broken', 'water', 'frozen', 'unresponsive']
# Collecting the user's problem using user input
problem = input(str("Please enter your problem:")).split()# Assigns the user input to the variable 'progarm' and converts the user input into a string
# Presenting solutions to user if corresponding Keywords are present in the users input 'problem'
if any(i in Keywords or Keywords2 for i in problem): # This while loop checks that any of the keywords (i) in the lists (Keyword or Keywords2) are present in the user's input (problem), which has been splitted into individual words (indicated by spacing - i)
if Keywords[0] or Keywords[1] and Keywords2[0] or Keywords2[1] in problem:
read(0)
read(1)
elif Keywords[0] or Keywords[1] and Keywords2[3] or Keywords2[4] in problem:
read(3)
elif Keywords2[2] in problem:
read(2)
elif Keywords[2] or Keywords[3] and Keywords2[5] or Keywords2[6] in problem:
read(4)
read(0)
当我输入'the phone fall in water'时,它应该检测关键字'water',即关键字2[2],并读取文本文件中的第二行。相反,它读取第一个if语句中指向的行。你知道吗
Please enter your problem:My phone fell into water
Get it repaired at a reliable repair shop
You will need to buy a new screen and a new screen protector
任何建议,以改善代码,使其工作将不胜感激!你知道吗
我认为问题可能与
in
的不当使用有关。你知道吗您可能需要执行以下操作:
而不是
类似地,对于内部
if
和elif
语句。你知道吗例如:
而不是
相关问题 更多 >
编程相关推荐