I'm playing on coding bat and
return str[:len(str) / 2]
works fine but
return str[:len(str) * 0.5]
returns
Error:slice indices must be integers or None or have an __index__ method
and although I can kind of speculate as to why this is I am very interested in hearing a better informed explanation. Thank you!
Bonus Points: Offer a tag I can place on this post so that we can categorize it well!
In Python 2:
Dividing two integers results in an integer.
Multiplying an integer and a float results in a float.
Slices must be integer.
In python, slices must be integers ( 5.0
is wrong, 5
is good).
len(str) / 5 # => integer
len(str) * 0.5 # => float
len(str) / 5 # => float
len(str) // 5 # => integer (equivalent of math.floor(len(str) / 5))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.