prob = input("Please enter your problem?")
words = set(prob.split())
file=open('solutions.txt')
line=file.readlines()
if ("smashed") or ("screen") or ("dropped") in words:
print (line[0])
elif ("wet") or ("water") in words:
print (line[6])
else:
print("sorry")
這段代碼的問題是,它只是打印文本文件的第一行如何打印包含用戶輸入的單詞的行?
這裏是結果:
>>>
============== RESTART: C:\Users\bb\Desktop\completed - Copy.py ==============
Please enter your problem?smashed
your screen is smashed
>>>
============== RESTART: C:\Users\bb\Desktop\completed - Copy.py ==============
Please enter your problem?wet
your screen is smashed
>>>
============== RESTART: C:\Users\bb\Desktop\completed - Copy.py ==============
Please enter your problem?bntuib
your screen is smashed
>>>
正如你可以看到它只能顯示代碼的最上面一行不管用戶輸入什麼。
'如果(「被砸「)或(」屏幕「)或(」放棄「):」這不是你想要的。如果(「粉碎」)總是評估爲「真」。你想要:「如果」用文字或「屏幕」砸碎了「單詞」或「丟棄」單詞:'(並相應地修復'elif' – UnholySheep
@UnholySheep謝謝! – error404