首先,我从一个文本文件中提取一个被分配给clientID的强度,一旦我有了这个强度,我将它存储为clientIntensityResult,然后根据它是否等于高或中等,它应该打印出相应的列表,但我在if语句中的比较似乎不起作用?我已将其更改为x = 1,如果x == 1:这可行,但在此字符串比较中有一些我缺少的东西?
def clientSelect():
clientIntensity = open("clientIntensity.txt", "r") #opens file
high = ["Running", "Swimming", "Aerobics", "Football", "Tennis"]
moderate = ["Walking", "Hiking", "Cleaning", "Skateboarding", "Basketball"]
searchId = input("Please enter a clientID (ffsss): ") #search string
for line in clientIntensity:
if re.match(searchId, line):
clientIntensityResult = (line[6:])
print ("Client intensity is: ", clientIntensityResult)
if clientIntensityResult == "High":
print (high)
elif clientIntensityResult == "Moderate":
print (moderate)
else:
print ("Nothing found")
答案 0 :(得分:2)
您将变量定义为
clientIntensityResult = (line[6:])
由于您是逐行迭代的,因此每个字符串的末尾都会有一个'\n'
。你可以做到
clientIntensityResult = line.strip()[6:]
其余部分应该有效。