Why do i need to write (answers[random.randint(0, len(answers) - 1)] instead of random.choice?
up vote
-2
down vote
favorite
Hello everyone I learned the basics of python.
I wanted to start a project on a magic8ball.
It didn't worked so I searched a bit and found a working code.
He uses this weird thing and I don't know why.
Can someone explain what this means?
python
add a comment |
up vote
-2
down vote
favorite
Hello everyone I learned the basics of python.
I wanted to start a project on a magic8ball.
It didn't worked so I searched a bit and found a working code.
He uses this weird thing and I don't know why.
Can someone explain what this means?
python
3
Absolutely no way for us to answer this without seeing the code.
– roganjosh
Nov 10 at 21:07
1
You couldnt userandom.choiche
, though, because that's a typo
– roganjosh
Nov 10 at 21:09
Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 10 at 21:12
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Hello everyone I learned the basics of python.
I wanted to start a project on a magic8ball.
It didn't worked so I searched a bit and found a working code.
He uses this weird thing and I don't know why.
Can someone explain what this means?
python
Hello everyone I learned the basics of python.
I wanted to start a project on a magic8ball.
It didn't worked so I searched a bit and found a working code.
He uses this weird thing and I don't know why.
Can someone explain what this means?
python
python
edited Nov 11 at 11:02
quant
1,43811226
1,43811226
asked Nov 10 at 20:54
Achilles
21
21
3
Absolutely no way for us to answer this without seeing the code.
– roganjosh
Nov 10 at 21:07
1
You couldnt userandom.choiche
, though, because that's a typo
– roganjosh
Nov 10 at 21:09
Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 10 at 21:12
add a comment |
3
Absolutely no way for us to answer this without seeing the code.
– roganjosh
Nov 10 at 21:07
1
You couldnt userandom.choiche
, though, because that's a typo
– roganjosh
Nov 10 at 21:09
Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 10 at 21:12
3
3
Absolutely no way for us to answer this without seeing the code.
– roganjosh
Nov 10 at 21:07
Absolutely no way for us to answer this without seeing the code.
– roganjosh
Nov 10 at 21:07
1
1
You couldnt use
random.choiche
, though, because that's a typo– roganjosh
Nov 10 at 21:09
You couldnt use
random.choiche
, though, because that's a typo– roganjosh
Nov 10 at 21:09
Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 10 at 21:12
Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 10 at 21:12
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
choice()
returns a random item from a list, tuple, or string while randint()
returns a pseudorandom integer between a and b. You can use either, assuming you are passing it the right parameters.
Say you have
a = [1, 2, 3, 4, 5, 6, 7]
import random
random.choice(a) # will return a random number from that list
random.randint(1,7) # will return a random number between 1 and 7 itself
In your case,
answers[randint(0, len(answers) - 1)]
will give you a random integer between 0 and however long the list of answers is minus one.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
choice()
returns a random item from a list, tuple, or string while randint()
returns a pseudorandom integer between a and b. You can use either, assuming you are passing it the right parameters.
Say you have
a = [1, 2, 3, 4, 5, 6, 7]
import random
random.choice(a) # will return a random number from that list
random.randint(1,7) # will return a random number between 1 and 7 itself
In your case,
answers[randint(0, len(answers) - 1)]
will give you a random integer between 0 and however long the list of answers is minus one.
add a comment |
up vote
0
down vote
choice()
returns a random item from a list, tuple, or string while randint()
returns a pseudorandom integer between a and b. You can use either, assuming you are passing it the right parameters.
Say you have
a = [1, 2, 3, 4, 5, 6, 7]
import random
random.choice(a) # will return a random number from that list
random.randint(1,7) # will return a random number between 1 and 7 itself
In your case,
answers[randint(0, len(answers) - 1)]
will give you a random integer between 0 and however long the list of answers is minus one.
add a comment |
up vote
0
down vote
up vote
0
down vote
choice()
returns a random item from a list, tuple, or string while randint()
returns a pseudorandom integer between a and b. You can use either, assuming you are passing it the right parameters.
Say you have
a = [1, 2, 3, 4, 5, 6, 7]
import random
random.choice(a) # will return a random number from that list
random.randint(1,7) # will return a random number between 1 and 7 itself
In your case,
answers[randint(0, len(answers) - 1)]
will give you a random integer between 0 and however long the list of answers is minus one.
choice()
returns a random item from a list, tuple, or string while randint()
returns a pseudorandom integer between a and b. You can use either, assuming you are passing it the right parameters.
Say you have
a = [1, 2, 3, 4, 5, 6, 7]
import random
random.choice(a) # will return a random number from that list
random.randint(1,7) # will return a random number between 1 and 7 itself
In your case,
answers[randint(0, len(answers) - 1)]
will give you a random integer between 0 and however long the list of answers is minus one.
answered Nov 10 at 21:32
Mister Tusk
1199
1199
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243317%2fwhy-do-i-need-to-write-answersrandom-randint0-lenanswers-1-instead-of%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Absolutely no way for us to answer this without seeing the code.
– roganjosh
Nov 10 at 21:07
1
You couldnt use
random.choiche
, though, because that's a typo– roganjosh
Nov 10 at 21:09
Welcome to stackoverflow! Please take the tour and read the help pages. Helpful may be "how to ask good questions" and this question checklist. Users here are way more ready to help if you provide minimal, complete, and verifiable example with some input and the desired output.
– Mikhail Stepanov
Nov 10 at 21:12