首先,我从文本文件中提取分配给clientID的强度,一旦获得强度,便将其存储为clientIntensityResult,然后根据其等于High还是Moderate来打印相应的强度列表,但是我在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")
最佳答案
您将变量定义为
clientIntensityResult = (line[6:])
由于您要逐行进行迭代,因此每个字符串的末尾都有一个
'\n'
。你可以做clientIntensityResult = line.strip()[6:]
然后其余的应该工作。