Python 3 need help in a school project












4














So I'm taking a programming course in high school, right now and I am making a program of a game that the teacher assigned for all of us make. The game is called "game of sticks" (if you would like a better run down on how the game works skip about half way through this video https://www.youtube.com/watch?v=dUXW3Kh_kxo&t=280s). Basically we have 15 sticks on a table and you can take away 1, 2, or 3 sticks. I have a code that works for the game but it says I'm doing an illegal move for 5 sticks left with any play. I cant find the problem and was hoping someone else could.



pl1 = input("Player 1, what is your username?") #player 1
pl2 = input("Player 2, what is your username?") #player 2
turnsa = 0 #player1 turns
turnsb = 0 #player2 turns
x = 15 #number of sticks
whichplayer = 1
while(x != 1):
while(whichplayer == 1):
P1 = int(input(pl1 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P1 < x and P1 < 4: # check for legal move
x = x - P1
turnsa = turnsa + 1
whichplayer = 2 #ending loop to start player 2 turn
if P1 > 3 or P1 > x: #check for illegal move
print('illegal move')
continue #restarting player 1 loop
while(whichplayer == 2):
P2 = int(input(pl2 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P2 < x and P2 < 4:
x = x - P2
turnsb = turnsb + 1
whichplayer = 1
if P2 > 3 or P2 > x:
print('illegal move')
continue
if turnsa > turnsb:
print('congrats ' + pl1 + ' you win')
if turnsb > turnsa:
print('congrats ' + pl2 + ' you win')









share|improve this question




















  • 1




    Could you provide a sample of input values that would provide the issue you are talking about?
    – M.G
    Nov 23 at 0:31










  • There is more than one "illegal move rule". It will help if you print "illegal move 1" and "illegal move 2". See the behavior and check which rules are troublesome.
    – Francio Rodrigues
    Nov 23 at 0:34










  • could you elaborate i'm not sure what you mean
    – jake millican
    Nov 23 at 0:38










  • and @M.G the game starts with 15 sticks and if you open this link repl.it/repls/PlasticAmbitiousComputeranimation and run the program it will always say illegal move when there is 5 sticks left and the guess is 3 it prints "illegal move" and when the amount of sticks is less than 5 it just repeats the question with nothing actually happening
    – jake millican
    Nov 23 at 0:43
















4














So I'm taking a programming course in high school, right now and I am making a program of a game that the teacher assigned for all of us make. The game is called "game of sticks" (if you would like a better run down on how the game works skip about half way through this video https://www.youtube.com/watch?v=dUXW3Kh_kxo&t=280s). Basically we have 15 sticks on a table and you can take away 1, 2, or 3 sticks. I have a code that works for the game but it says I'm doing an illegal move for 5 sticks left with any play. I cant find the problem and was hoping someone else could.



pl1 = input("Player 1, what is your username?") #player 1
pl2 = input("Player 2, what is your username?") #player 2
turnsa = 0 #player1 turns
turnsb = 0 #player2 turns
x = 15 #number of sticks
whichplayer = 1
while(x != 1):
while(whichplayer == 1):
P1 = int(input(pl1 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P1 < x and P1 < 4: # check for legal move
x = x - P1
turnsa = turnsa + 1
whichplayer = 2 #ending loop to start player 2 turn
if P1 > 3 or P1 > x: #check for illegal move
print('illegal move')
continue #restarting player 1 loop
while(whichplayer == 2):
P2 = int(input(pl2 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P2 < x and P2 < 4:
x = x - P2
turnsb = turnsb + 1
whichplayer = 1
if P2 > 3 or P2 > x:
print('illegal move')
continue
if turnsa > turnsb:
print('congrats ' + pl1 + ' you win')
if turnsb > turnsa:
print('congrats ' + pl2 + ' you win')









share|improve this question




















  • 1




    Could you provide a sample of input values that would provide the issue you are talking about?
    – M.G
    Nov 23 at 0:31










  • There is more than one "illegal move rule". It will help if you print "illegal move 1" and "illegal move 2". See the behavior and check which rules are troublesome.
    – Francio Rodrigues
    Nov 23 at 0:34










  • could you elaborate i'm not sure what you mean
    – jake millican
    Nov 23 at 0:38










  • and @M.G the game starts with 15 sticks and if you open this link repl.it/repls/PlasticAmbitiousComputeranimation and run the program it will always say illegal move when there is 5 sticks left and the guess is 3 it prints "illegal move" and when the amount of sticks is less than 5 it just repeats the question with nothing actually happening
    – jake millican
    Nov 23 at 0:43














4












4








4







So I'm taking a programming course in high school, right now and I am making a program of a game that the teacher assigned for all of us make. The game is called "game of sticks" (if you would like a better run down on how the game works skip about half way through this video https://www.youtube.com/watch?v=dUXW3Kh_kxo&t=280s). Basically we have 15 sticks on a table and you can take away 1, 2, or 3 sticks. I have a code that works for the game but it says I'm doing an illegal move for 5 sticks left with any play. I cant find the problem and was hoping someone else could.



pl1 = input("Player 1, what is your username?") #player 1
pl2 = input("Player 2, what is your username?") #player 2
turnsa = 0 #player1 turns
turnsb = 0 #player2 turns
x = 15 #number of sticks
whichplayer = 1
while(x != 1):
while(whichplayer == 1):
P1 = int(input(pl1 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P1 < x and P1 < 4: # check for legal move
x = x - P1
turnsa = turnsa + 1
whichplayer = 2 #ending loop to start player 2 turn
if P1 > 3 or P1 > x: #check for illegal move
print('illegal move')
continue #restarting player 1 loop
while(whichplayer == 2):
P2 = int(input(pl2 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P2 < x and P2 < 4:
x = x - P2
turnsb = turnsb + 1
whichplayer = 1
if P2 > 3 or P2 > x:
print('illegal move')
continue
if turnsa > turnsb:
print('congrats ' + pl1 + ' you win')
if turnsb > turnsa:
print('congrats ' + pl2 + ' you win')









share|improve this question















So I'm taking a programming course in high school, right now and I am making a program of a game that the teacher assigned for all of us make. The game is called "game of sticks" (if you would like a better run down on how the game works skip about half way through this video https://www.youtube.com/watch?v=dUXW3Kh_kxo&t=280s). Basically we have 15 sticks on a table and you can take away 1, 2, or 3 sticks. I have a code that works for the game but it says I'm doing an illegal move for 5 sticks left with any play. I cant find the problem and was hoping someone else could.



pl1 = input("Player 1, what is your username?") #player 1
pl2 = input("Player 2, what is your username?") #player 2
turnsa = 0 #player1 turns
turnsb = 0 #player2 turns
x = 15 #number of sticks
whichplayer = 1
while(x != 1):
while(whichplayer == 1):
P1 = int(input(pl1 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P1 < x and P1 < 4: # check for legal move
x = x - P1
turnsa = turnsa + 1
whichplayer = 2 #ending loop to start player 2 turn
if P1 > 3 or P1 > x: #check for illegal move
print('illegal move')
continue #restarting player 1 loop
while(whichplayer == 2):
P2 = int(input(pl2 + ', choose an amount of sticks from 1-3 ' + str(x) +
' sticks remaining'))
if P2 < x and P2 < 4:
x = x - P2
turnsb = turnsb + 1
whichplayer = 1
if P2 > 3 or P2 > x:
print('illegal move')
continue
if turnsa > turnsb:
print('congrats ' + pl1 + ' you win')
if turnsb > turnsa:
print('congrats ' + pl2 + ' you win')






python python-3.x question2answer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 3:07









Josh21

638




638










asked Nov 23 at 0:21









jake millican

232




232








  • 1




    Could you provide a sample of input values that would provide the issue you are talking about?
    – M.G
    Nov 23 at 0:31










  • There is more than one "illegal move rule". It will help if you print "illegal move 1" and "illegal move 2". See the behavior and check which rules are troublesome.
    – Francio Rodrigues
    Nov 23 at 0:34










  • could you elaborate i'm not sure what you mean
    – jake millican
    Nov 23 at 0:38










  • and @M.G the game starts with 15 sticks and if you open this link repl.it/repls/PlasticAmbitiousComputeranimation and run the program it will always say illegal move when there is 5 sticks left and the guess is 3 it prints "illegal move" and when the amount of sticks is less than 5 it just repeats the question with nothing actually happening
    – jake millican
    Nov 23 at 0:43














  • 1




    Could you provide a sample of input values that would provide the issue you are talking about?
    – M.G
    Nov 23 at 0:31










  • There is more than one "illegal move rule". It will help if you print "illegal move 1" and "illegal move 2". See the behavior and check which rules are troublesome.
    – Francio Rodrigues
    Nov 23 at 0:34










  • could you elaborate i'm not sure what you mean
    – jake millican
    Nov 23 at 0:38










  • and @M.G the game starts with 15 sticks and if you open this link repl.it/repls/PlasticAmbitiousComputeranimation and run the program it will always say illegal move when there is 5 sticks left and the guess is 3 it prints "illegal move" and when the amount of sticks is less than 5 it just repeats the question with nothing actually happening
    – jake millican
    Nov 23 at 0:43








1




1




Could you provide a sample of input values that would provide the issue you are talking about?
– M.G
Nov 23 at 0:31




Could you provide a sample of input values that would provide the issue you are talking about?
– M.G
Nov 23 at 0:31












There is more than one "illegal move rule". It will help if you print "illegal move 1" and "illegal move 2". See the behavior and check which rules are troublesome.
– Francio Rodrigues
Nov 23 at 0:34




There is more than one "illegal move rule". It will help if you print "illegal move 1" and "illegal move 2". See the behavior and check which rules are troublesome.
– Francio Rodrigues
Nov 23 at 0:34












could you elaborate i'm not sure what you mean
– jake millican
Nov 23 at 0:38




could you elaborate i'm not sure what you mean
– jake millican
Nov 23 at 0:38












and @M.G the game starts with 15 sticks and if you open this link repl.it/repls/PlasticAmbitiousComputeranimation and run the program it will always say illegal move when there is 5 sticks left and the guess is 3 it prints "illegal move" and when the amount of sticks is less than 5 it just repeats the question with nothing actually happening
– jake millican
Nov 23 at 0:43




and @M.G the game starts with 15 sticks and if you open this link repl.it/repls/PlasticAmbitiousComputeranimation and run the program it will always say illegal move when there is 5 sticks left and the guess is 3 it prints "illegal move" and when the amount of sticks is less than 5 it just repeats the question with nothing actually happening
– jake millican
Nov 23 at 0:43












1 Answer
1






active

oldest

votes


















2














You are decreasing x in the first if statement, then when the second if checks P1 > x, it's true because you set x from 5 to 2, and the P1 is 3. You could just use an if/else here since if the move is not valid, you already know it will be an invalid move. Also you have some edge cases that need checking with your >, <, and != comparisons (try taking 3 sticks each turn and see what happens). I will leave the rest for you to work out as this is school work and will be a good learning experience. Will edit later if assistance is needed.



if P1 < x and P1 < 4: # check for legal move
x = x - P1 # <========= decrementing here then checking it in the next if
turnsa = turnsa + 1
whichplayer = 2 #ending loop to start player 2 turn
if P1 > 3 or P1 > x: #check for illegal move
print('illegal move')
continue #restarting player 1 loop





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',
    autoActivateHeartbeat: false,
    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%2f53439343%2fpython-3-need-help-in-a-school-project%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









    2














    You are decreasing x in the first if statement, then when the second if checks P1 > x, it's true because you set x from 5 to 2, and the P1 is 3. You could just use an if/else here since if the move is not valid, you already know it will be an invalid move. Also you have some edge cases that need checking with your >, <, and != comparisons (try taking 3 sticks each turn and see what happens). I will leave the rest for you to work out as this is school work and will be a good learning experience. Will edit later if assistance is needed.



    if P1 < x and P1 < 4: # check for legal move
    x = x - P1 # <========= decrementing here then checking it in the next if
    turnsa = turnsa + 1
    whichplayer = 2 #ending loop to start player 2 turn
    if P1 > 3 or P1 > x: #check for illegal move
    print('illegal move')
    continue #restarting player 1 loop





    share|improve this answer




























      2














      You are decreasing x in the first if statement, then when the second if checks P1 > x, it's true because you set x from 5 to 2, and the P1 is 3. You could just use an if/else here since if the move is not valid, you already know it will be an invalid move. Also you have some edge cases that need checking with your >, <, and != comparisons (try taking 3 sticks each turn and see what happens). I will leave the rest for you to work out as this is school work and will be a good learning experience. Will edit later if assistance is needed.



      if P1 < x and P1 < 4: # check for legal move
      x = x - P1 # <========= decrementing here then checking it in the next if
      turnsa = turnsa + 1
      whichplayer = 2 #ending loop to start player 2 turn
      if P1 > 3 or P1 > x: #check for illegal move
      print('illegal move')
      continue #restarting player 1 loop





      share|improve this answer


























        2












        2








        2






        You are decreasing x in the first if statement, then when the second if checks P1 > x, it's true because you set x from 5 to 2, and the P1 is 3. You could just use an if/else here since if the move is not valid, you already know it will be an invalid move. Also you have some edge cases that need checking with your >, <, and != comparisons (try taking 3 sticks each turn and see what happens). I will leave the rest for you to work out as this is school work and will be a good learning experience. Will edit later if assistance is needed.



        if P1 < x and P1 < 4: # check for legal move
        x = x - P1 # <========= decrementing here then checking it in the next if
        turnsa = turnsa + 1
        whichplayer = 2 #ending loop to start player 2 turn
        if P1 > 3 or P1 > x: #check for illegal move
        print('illegal move')
        continue #restarting player 1 loop





        share|improve this answer














        You are decreasing x in the first if statement, then when the second if checks P1 > x, it's true because you set x from 5 to 2, and the P1 is 3. You could just use an if/else here since if the move is not valid, you already know it will be an invalid move. Also you have some edge cases that need checking with your >, <, and != comparisons (try taking 3 sticks each turn and see what happens). I will leave the rest for you to work out as this is school work and will be a good learning experience. Will edit later if assistance is needed.



        if P1 < x and P1 < 4: # check for legal move
        x = x - P1 # <========= decrementing here then checking it in the next if
        turnsa = turnsa + 1
        whichplayer = 2 #ending loop to start player 2 turn
        if P1 > 3 or P1 > x: #check for illegal move
        print('illegal move')
        continue #restarting player 1 loop






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 at 1:01

























        answered Nov 23 at 0:54









        M.G

        398310




        398310






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439343%2fpython-3-need-help-in-a-school-project%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)