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?










share|improve this question




















  • 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















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?










share|improve this question




















  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 21:32









        Mister Tusk

        1199




        1199






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine