简体   繁体   English

为什么我的字符串 function [str()] 工作不正常?

[英]Why is my string function [str()] working incorrectly?

I'm trying to print the nth term of a string, but beyond a certain term this doesn't work.我正在尝试打印字符串的第 n 个术语,但超出某个术语这不起作用。

I set up my number to be such that the 2nd term is 2, 3rd term is 3 etc. 16th term is 6. This all works correctly up until term 17, where a number 8 is printed which doesn't make any sense.我将我的号码设置为第二个学期是 2,第三个学期是 3 等等。第 16 个学期是 6。这一切都正常工作,直到第 17 个学期,其中打印了一个没有任何意义的数字 8。 Strangely, this is working correctly for the first 16 terms but the 17th it gets wrong.奇怪的是,这在前 16 个术语中工作正常,但在第 17 个术语中却出错了。 When I try to print the 18th term, I get an error saying the string index is out of range, which it clearly isn't.当我尝试打印第 18 个术语时,我收到一条错误消息,指出字符串索引超出范围,但显然不是。

print(str(0.2345678901234567890)[17])

Interestingly, if I go with the notation:有趣的是,如果我 go 带有符号:

 print("0.2345678901234567890"[17]) 

then it works perfectly well with every element being printed correctly.那么它在正确打印每个元素的情况下都能很好地工作。 Hence I believe there is something wrong with the str() function, but I can't find anything online.因此我认为 str() function 有问题,但我在网上找不到任何东西。

You should check the results of appying str() :您应该检查应用str()的结果:

>>> str(0.2345678901234567890)
'0.234567890123'

it rounds up the number, and there are not enough digits to get #17.它将数字四舍五入,并且没有足够的数字来获得#17。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM