我正在製作一個故障排除程序,它將要求用戶輸入,搜索某些列表以找出問題並給出解決方案。避免打印相同的輸出
f=open('problem.txt')
lines=f.readlines()
problem1 = ["cracked", "phone", "screen", "dropped"]
problem2 = ["charging", "port", "phone"]
problem3 = ["slow", "phone", "freeze"]
problem_input = input ("What is your problem? ")
list_split = (problem_input.split())
for i in problem1:
if i in list_split:
print (lines[0])
for i in problem2:
if i in list_split:
print (lines[1])
但是,如果我輸入,"my phone is cracked"
,輸出將被打印兩次。我如何只打印一次?
它打印了兩次,因爲它與第一循環和第二循環的相符程度。您可以實現AND條件邏輯或使用break –