我是Python的初学者,我遇到的问题对我来说是新鲜事。我不太确定。
import sys
import time
import string
def programExit():
print("Thanks for using!")
time.sleep(0.75)
pressEnter = input("Press <ENTER> to exit the program.")
print("REMINDER: Please do not include special characters (i.e. @, £, >) in your problem.")
time.sleep(2)
userInput = input("What is the problem?: ")
removePunc = set(string.punctuation)
userInput = ''.join(ch for ch in userInput if ch not in removePunc)
userInput = userInput.lower()
userInput = userInput.split()
def solutionBattery():
print("From what I can tell, there is a problem with your battery. There" \
" seems to be multiple fixes for you issue. The first fix is: " \
"'Place your phone on charge.' The second fix is: 'Get our battery " \
"replaced or repaired by a technician.")
batteryList = ['battery', 'low', 'charge', 'depleted', 'percentage']
screenList = ['screen', 'display', 'cracked', 'broken', 'scratched']
softwareList = ['slow', 'unresponsive', 'respond', 'wifi', 'data', 'crashing']
storageList = ['storage', 'space', 'full', 'room', 'volume']
if batteryList in userInput == True:
print("Getting solution...")
time.sleep(1.25)
print(solutionBattery())
time.sleep(7)
programExit()
它似乎不想打印我所拥有的测试解决方案,而且我不确定为什么它不会这样做。我试过在这里寻找其他方面的答案,但我无法找到与我的问题相关的任何内容。希望你们能帮助我吗?干杯
答案 0 :(得分:0)
问题在于您的if
声明。您评估为False
batteryList in userInput #False
userInput == True #False
所以你的if
阻止没有运行。
userInput == True
这不是必需的,因为userInput
中<{1}} 与<{1}}不相同,但评估到True
。
True
首先,这是错误的方法。它应该是batteryList in userInput
。但是,这仍然会失败,因为您之前分开了用户的输入:
userInput in batteryList
这会返回一个列表。目前userInput = userInput.split()
= userInput
,这不在["battery"]
中。但是:batteryList
是,所以我们只需要从用户的输入中获取第一项:
"battery"