Python - How do you exit a program if a condition is met?











up vote
-1
down vote

favorite












I can't exit the program when the condition at start of the code is met and it also prevents the rest of the code from working when it is not met. However if you remove the conditions from the start the code works fine. How do I get the conditions at the start to work properly? P.S I know the code can be condensed.



import sys  
user1 = (input("User 1 type your name " )).lower
user2 = (input("User 2 type your name ")).lower
if user1 != "bob":
sys.exit()
if user2 != "fred":
sys.exit()

from random import randint
total = 1
score1 = 0
score2 = 0
while total <6:
roll = (input("User 1 x press 1 to roll the dice "))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice "))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

total = total+1
if total == 6:
print("nUser1 your total score is",score1)
print("User2 your total score is",score2)

while total >= 6:
if score1 == score2:
print("It's a tie! Whoever rolls the highest number wins")
roll = (input("User 1 press x to roll the dice"))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice"))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

if score1 > score2:
print("nUser 1 wins")
break
if score1 < score2:
print("nUser 2 wins")
break









share|improve this question






















  • Could you explain what you want the conditions to do, and what they are currently doing?
    – jdrd
    Nov 19 at 23:01










  • lower is a method, you need to call it
    – Daniel Roseman
    Nov 19 at 23:02










  • What are those "conditions at the start"? The two if user.. sections?
    – usr2564301
    Nov 19 at 23:03










  • This was already answered here: stackoverflow.com/questions/17179615/…
    – Tobias Wilfert
    Nov 19 at 23:03






  • 1




    you need to call the string methods, using parentheses: user1 = (input("User 1 type your name " )).lower()
    – juanpa.arrivillaga
    Nov 19 at 23:04















up vote
-1
down vote

favorite












I can't exit the program when the condition at start of the code is met and it also prevents the rest of the code from working when it is not met. However if you remove the conditions from the start the code works fine. How do I get the conditions at the start to work properly? P.S I know the code can be condensed.



import sys  
user1 = (input("User 1 type your name " )).lower
user2 = (input("User 2 type your name ")).lower
if user1 != "bob":
sys.exit()
if user2 != "fred":
sys.exit()

from random import randint
total = 1
score1 = 0
score2 = 0
while total <6:
roll = (input("User 1 x press 1 to roll the dice "))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice "))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

total = total+1
if total == 6:
print("nUser1 your total score is",score1)
print("User2 your total score is",score2)

while total >= 6:
if score1 == score2:
print("It's a tie! Whoever rolls the highest number wins")
roll = (input("User 1 press x to roll the dice"))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice"))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

if score1 > score2:
print("nUser 1 wins")
break
if score1 < score2:
print("nUser 2 wins")
break









share|improve this question






















  • Could you explain what you want the conditions to do, and what they are currently doing?
    – jdrd
    Nov 19 at 23:01










  • lower is a method, you need to call it
    – Daniel Roseman
    Nov 19 at 23:02










  • What are those "conditions at the start"? The two if user.. sections?
    – usr2564301
    Nov 19 at 23:03










  • This was already answered here: stackoverflow.com/questions/17179615/…
    – Tobias Wilfert
    Nov 19 at 23:03






  • 1




    you need to call the string methods, using parentheses: user1 = (input("User 1 type your name " )).lower()
    – juanpa.arrivillaga
    Nov 19 at 23:04













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I can't exit the program when the condition at start of the code is met and it also prevents the rest of the code from working when it is not met. However if you remove the conditions from the start the code works fine. How do I get the conditions at the start to work properly? P.S I know the code can be condensed.



import sys  
user1 = (input("User 1 type your name " )).lower
user2 = (input("User 2 type your name ")).lower
if user1 != "bob":
sys.exit()
if user2 != "fred":
sys.exit()

from random import randint
total = 1
score1 = 0
score2 = 0
while total <6:
roll = (input("User 1 x press 1 to roll the dice "))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice "))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

total = total+1
if total == 6:
print("nUser1 your total score is",score1)
print("User2 your total score is",score2)

while total >= 6:
if score1 == score2:
print("It's a tie! Whoever rolls the highest number wins")
roll = (input("User 1 press x to roll the dice"))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice"))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

if score1 > score2:
print("nUser 1 wins")
break
if score1 < score2:
print("nUser 2 wins")
break









share|improve this question













I can't exit the program when the condition at start of the code is met and it also prevents the rest of the code from working when it is not met. However if you remove the conditions from the start the code works fine. How do I get the conditions at the start to work properly? P.S I know the code can be condensed.



import sys  
user1 = (input("User 1 type your name " )).lower
user2 = (input("User 2 type your name ")).lower
if user1 != "bob":
sys.exit()
if user2 != "fred":
sys.exit()

from random import randint
total = 1
score1 = 0
score2 = 0
while total <6:
roll = (input("User 1 x press 1 to roll the dice "))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice "))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

total = total+1
if total == 6:
print("nUser1 your total score is",score1)
print("User2 your total score is",score2)

while total >= 6:
if score1 == score2:
print("It's a tie! Whoever rolls the highest number wins")
roll = (input("User 1 press x to roll the dice"))
if roll == "x":
dice = randint(1,6)
print("You got",dice)
score1 = score1+dice
print("User 1 your score is",score1)

roll2 = (input("nUser 2 press x to roll the dice"))
if roll2 == "x":
dice = randint(1,6)
print("You got",dice)
score2 = score2+dice
print("User 2 your score is",score2)

if score1 > score2:
print("nUser 1 wins")
break
if score1 < score2:
print("nUser 2 wins")
break






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 22:58









Jack

101




101












  • Could you explain what you want the conditions to do, and what they are currently doing?
    – jdrd
    Nov 19 at 23:01










  • lower is a method, you need to call it
    – Daniel Roseman
    Nov 19 at 23:02










  • What are those "conditions at the start"? The two if user.. sections?
    – usr2564301
    Nov 19 at 23:03










  • This was already answered here: stackoverflow.com/questions/17179615/…
    – Tobias Wilfert
    Nov 19 at 23:03






  • 1




    you need to call the string methods, using parentheses: user1 = (input("User 1 type your name " )).lower()
    – juanpa.arrivillaga
    Nov 19 at 23:04


















  • Could you explain what you want the conditions to do, and what they are currently doing?
    – jdrd
    Nov 19 at 23:01










  • lower is a method, you need to call it
    – Daniel Roseman
    Nov 19 at 23:02










  • What are those "conditions at the start"? The two if user.. sections?
    – usr2564301
    Nov 19 at 23:03










  • This was already answered here: stackoverflow.com/questions/17179615/…
    – Tobias Wilfert
    Nov 19 at 23:03






  • 1




    you need to call the string methods, using parentheses: user1 = (input("User 1 type your name " )).lower()
    – juanpa.arrivillaga
    Nov 19 at 23:04
















Could you explain what you want the conditions to do, and what they are currently doing?
– jdrd
Nov 19 at 23:01




Could you explain what you want the conditions to do, and what they are currently doing?
– jdrd
Nov 19 at 23:01












lower is a method, you need to call it
– Daniel Roseman
Nov 19 at 23:02




lower is a method, you need to call it
– Daniel Roseman
Nov 19 at 23:02












What are those "conditions at the start"? The two if user.. sections?
– usr2564301
Nov 19 at 23:03




What are those "conditions at the start"? The two if user.. sections?
– usr2564301
Nov 19 at 23:03












This was already answered here: stackoverflow.com/questions/17179615/…
– Tobias Wilfert
Nov 19 at 23:03




This was already answered here: stackoverflow.com/questions/17179615/…
– Tobias Wilfert
Nov 19 at 23:03




1




1




you need to call the string methods, using parentheses: user1 = (input("User 1 type your name " )).lower()
– juanpa.arrivillaga
Nov 19 at 23:04




you need to call the string methods, using parentheses: user1 = (input("User 1 type your name " )).lower()
– juanpa.arrivillaga
Nov 19 at 23:04












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










lower is actually a method, and you have to call it. Instead of writing .lower try writing .lower()






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%2f53383850%2fpython-how-do-you-exit-a-program-if-a-condition-is-met%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



    accepted










    lower is actually a method, and you have to call it. Instead of writing .lower try writing .lower()






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      lower is actually a method, and you have to call it. Instead of writing .lower try writing .lower()






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        lower is actually a method, and you have to call it. Instead of writing .lower try writing .lower()






        share|improve this answer












        lower is actually a method, and you have to call it. Instead of writing .lower try writing .lower()







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 23:03









        chromaerror

        15111




        15111






























            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%2f53383850%2fpython-how-do-you-exit-a-program-if-a-condition-is-met%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

            Wiesbaden

            Marschland

            Dieringhausen