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
python
|
show 3 more comments
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
python
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 twoif 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
|
show 3 more comments
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
python
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
python
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 twoif 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
|
show 3 more comments
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 twoif 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
|
show 3 more comments
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()
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
accepted
lower
is actually a method, and you have to call it. Instead of writing .lower
try writing .lower()
add a comment |
up vote
0
down vote
accepted
lower
is actually a method, and you have to call it. Instead of writing .lower
try writing .lower()
add a comment |
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()
lower
is actually a method, and you have to call it. Instead of writing .lower
try writing .lower()
answered Nov 19 at 23:03
chromaerror
15111
15111
add a comment |
add a comment |
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.
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%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
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
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