gpt4 book ai didi

Python 字符串比较似乎不起作用

转载 作者:行者123 更新时间:2023-12-01 04:50:47 25 4
gpt4 key购买 nike

首先,我从文本文件中提取分配给 clientID 的强度,一旦获得该强度,我将其存储为 clientIntensityResult,然后根据它是否等于高或中等,它应该打印出相应的列表,但我在 if 语句中的比较似乎不起作用?我已将其更改为 x = 1, if 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:]

那么剩下的就应该可以工作了。

关于Python 字符串比较似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28521336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com