#๐ Python len function confusion
40 messages ยท Page 1 of 1 (latest)
@noble osprey
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
the -1 isnt in the len function but rather the randint
sry i mean randint
the book says something about how python thinks index starts at 0
but I don't understand how the -1 still
!e
import random
help(random.randint)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | Help on method randint in module random:
002 |
003 | randint(a, b) method of random.Random instance
004 | Return random integer in range [a, b], including both end points.
if u look at the help for it, u will see how it includes both end points
unlike the builtin range
and because of this, if it rolled len(wordList) it would error
but wouldnt that only be an issue if you had 2 items in your list
if u had 2 items then len will return 2
since it has 2 items then the possible indexes are 0 (the first item) and 1 (the 2nd item)
there is no index 2
we've made a bot function for that-
!docs random.randint
random.randint(a, b)```
Return a random integer *N* such that `a <= N <= b`. Alias for `randrange(a, b+1)`.
ohhhhhh
the bot isnt avaible in ur local repl
cant u just do !d 
index starts from 0; for example there could be an element at the 0th position and the 1st position.
whereas the len() function (for length) counts the amount of element/s present.
which means that for the previous example, it'd return 2 - since there are 2 elements.
yes. yes you can. iirc even !documentation work as well .
also might be just me but the help() one for this sounds more friendly to beginners
.close
woops
sry didnt mean
to tag for that
its with a !
ohhh right
!close
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.