a=input("Please enter your problem?")
problem = a.split(' ')
max_num, current_num = 0,0
chosen_line = ''
with open('solutions.txt', 'r') as searchfile:
for line in searchfile:
for word in problem:
if word in line:
current_num+=1
if current_num>max_num:
max_num=current_num
chosen_line = line
print (chosen_line)
else:
print ("please try again")
此代碼打印文本文件中的所有行,但我只需要它打印大多數單詞的行,用戶已輸入。此外,如果它沒有找到任何用戶輸入的單詞在它應該顯示「請重試」,但是它顯示其7倍如何讓python用大多數單詞打印行並停止重複打印?
我沒有看到任何代碼逼近您描述的任務。 – TigerhawkT3
要停止循環,請使用'break'。但你不想那樣。你只需要在循環內打印任何東西 –