|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目要求是Write code using find() and string slicing to extract the number at the end of the line below(text = "X-DSPAM-Confidence: 0.8475"). Convert the extracted value to a floating point number and print it out.
我写的code是
text = "X-DSPAM-Confidence: 0.8475";
a = data.find(" ",:)
print(a)
b = data.find(5)
print(b)
c = data[a+1:b+1]
print(c)
最后答案应该是0.8475
求问1)这代码第二行为啥有问题, 2)怎样改这段代码比较好, 谢谢🙏
本帖最后由 Twilight6 于 2020-7-8 10:25 编辑
[b]
find 第一个参数填的是需要查找的字符,第二个参数是开始的位置参数,第三个是结束的位置参数,都是指查找的范围,如果没填默认是整个字符串范围,所以这里的find 用法错了
你的 data 参数应该改成 text ,因为你data 都没有定义
最后一步列表切片是包含左端不包含右端的元素,所以应该改成 [a:b+1] ,还有题目要求提取出来后i转为浮点型,那么要加上 float 转化为浮点型下
- text = "X-DSPAM-Confidence: 0.8475"
- a = text.find("0")
- print(a)
- b = text.find("5")
- print(b)
- c = float(text[a:b+1])
- print(c)
复制代码
在这里单纯提取最后的数字这样改就行:
- text = "X-DSPAM-Confidence: 0.8475"
- a = text.find("0")
- c = float(text[a:])
- print(c)
复制代码
输出结果:
[/b]
|
|