How to end program if input == “quit” with many if statements?
I want the user to be able to quit this program at any point by typing in "quit".
Is there a way to do this with one instance of a break statement, or do I need to add a break to every "if y ==" statement in my code?
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
elif y == "clearfruits":
del fruits[:]
elif y == "clearnotfruits":
del notfruits[:]
elif y == "quit":
break
else:
print("Not a valid option!")
python loops if-statement break
add a comment |
I want the user to be able to quit this program at any point by typing in "quit".
Is there a way to do this with one instance of a break statement, or do I need to add a break to every "if y ==" statement in my code?
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
elif y == "clearfruits":
del fruits[:]
elif y == "clearnotfruits":
del notfruits[:]
elif y == "quit":
break
else:
print("Not a valid option!")
python loops if-statement break
3
You can create your ownmyInput
function which callsinput
and callssys.exit(0)
if it founds "quit" and returns input otherwise. Then you replace allinput
s bymyInput
.
– Michael Butscher
Nov 12 at 22:37
add a comment |
I want the user to be able to quit this program at any point by typing in "quit".
Is there a way to do this with one instance of a break statement, or do I need to add a break to every "if y ==" statement in my code?
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
elif y == "clearfruits":
del fruits[:]
elif y == "clearnotfruits":
del notfruits[:]
elif y == "quit":
break
else:
print("Not a valid option!")
python loops if-statement break
I want the user to be able to quit this program at any point by typing in "quit".
Is there a way to do this with one instance of a break statement, or do I need to add a break to every "if y ==" statement in my code?
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
elif y == "clearfruits":
del fruits[:]
elif y == "clearnotfruits":
del notfruits[:]
elif y == "quit":
break
else:
print("Not a valid option!")
python loops if-statement break
python loops if-statement break
asked Nov 12 at 22:34
Bobert
284
284
3
You can create your ownmyInput
function which callsinput
and callssys.exit(0)
if it founds "quit" and returns input otherwise. Then you replace allinput
s bymyInput
.
– Michael Butscher
Nov 12 at 22:37
add a comment |
3
You can create your ownmyInput
function which callsinput
and callssys.exit(0)
if it founds "quit" and returns input otherwise. Then you replace allinput
s bymyInput
.
– Michael Butscher
Nov 12 at 22:37
3
3
You can create your own
myInput
function which calls input
and calls sys.exit(0)
if it founds "quit" and returns input otherwise. Then you replace all input
s by myInput
.– Michael Butscher
Nov 12 at 22:37
You can create your own
myInput
function which calls input
and calls sys.exit(0)
if it founds "quit" and returns input otherwise. Then you replace all input
s by myInput
.– Michael Butscher
Nov 12 at 22:37
add a comment |
3 Answers
3
active
oldest
votes
Create a function, use it each time you taker an input, call "exit()" to leave
For example
import sys
def check_quit(inp):
if inp == 'quit':
sys.exit(0)
@michael-butscher has the best answere.
– Skapin
Nov 12 at 22:40
Thanks. Was hoping to only need to call the function once, but I just placed it with each instance of user input.
– Bobert
Nov 12 at 23:04
add a comment |
You can use
import sys
sys.exit(0)
to immediately stop executing further program statements, so something like
elif y == "quit":
import sys
sys.exit(0)
should work.
Documentation: https://docs.python.org/3.5/library/sys.html#sys.exit
used @michael-butscher solution, but your's is the same idea without the function. Thank you!
– Bobert
Nov 12 at 23:03
add a comment |
I think that both writing a function and using sys.exit
are overkill for what OP asked, depending whether you're trying to break out of the loop or exit the program entirely
Specifically regarding your question, you can break
right after your input()
and it will exit the loop without executing the rest of the run. (BTW, you don't need to cast input to a string, input is a string by default)
y = input(": ")
if y.lower() == "quit":
break
if y == "fruits":
This breaks the current loop and reverts back to the previous loop. Useful because I wanted to create a "back" step at each loop, but doesn't end the whole program until you get to the first loop and enter "quit". Thank you for answering!
– Bobert
Nov 12 at 23:05
I suppose I misunderstood your question, the solution breaks out of thewhile
loop if put right after the input. Hope it was helpful anyway
– G. Anderson
Nov 12 at 23:16
No problem. It was helpful, as I also wanted a way to break the current loop without exiting the whole program.
– Bobert
Nov 12 at 23:26
add a comment |
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
});
}
});
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%2f53271077%2fhow-to-end-program-if-input-quit-with-many-if-statements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Create a function, use it each time you taker an input, call "exit()" to leave
For example
import sys
def check_quit(inp):
if inp == 'quit':
sys.exit(0)
@michael-butscher has the best answere.
– Skapin
Nov 12 at 22:40
Thanks. Was hoping to only need to call the function once, but I just placed it with each instance of user input.
– Bobert
Nov 12 at 23:04
add a comment |
Create a function, use it each time you taker an input, call "exit()" to leave
For example
import sys
def check_quit(inp):
if inp == 'quit':
sys.exit(0)
@michael-butscher has the best answere.
– Skapin
Nov 12 at 22:40
Thanks. Was hoping to only need to call the function once, but I just placed it with each instance of user input.
– Bobert
Nov 12 at 23:04
add a comment |
Create a function, use it each time you taker an input, call "exit()" to leave
For example
import sys
def check_quit(inp):
if inp == 'quit':
sys.exit(0)
Create a function, use it each time you taker an input, call "exit()" to leave
For example
import sys
def check_quit(inp):
if inp == 'quit':
sys.exit(0)
edited Nov 12 at 22:39
Michael Butscher
4,23321321
4,23321321
answered Nov 12 at 22:37
Skapin
665
665
@michael-butscher has the best answere.
– Skapin
Nov 12 at 22:40
Thanks. Was hoping to only need to call the function once, but I just placed it with each instance of user input.
– Bobert
Nov 12 at 23:04
add a comment |
@michael-butscher has the best answere.
– Skapin
Nov 12 at 22:40
Thanks. Was hoping to only need to call the function once, but I just placed it with each instance of user input.
– Bobert
Nov 12 at 23:04
@michael-butscher has the best answere.
– Skapin
Nov 12 at 22:40
@michael-butscher has the best answere.
– Skapin
Nov 12 at 22:40
Thanks. Was hoping to only need to call the function once, but I just placed it with each instance of user input.
– Bobert
Nov 12 at 23:04
Thanks. Was hoping to only need to call the function once, but I just placed it with each instance of user input.
– Bobert
Nov 12 at 23:04
add a comment |
You can use
import sys
sys.exit(0)
to immediately stop executing further program statements, so something like
elif y == "quit":
import sys
sys.exit(0)
should work.
Documentation: https://docs.python.org/3.5/library/sys.html#sys.exit
used @michael-butscher solution, but your's is the same idea without the function. Thank you!
– Bobert
Nov 12 at 23:03
add a comment |
You can use
import sys
sys.exit(0)
to immediately stop executing further program statements, so something like
elif y == "quit":
import sys
sys.exit(0)
should work.
Documentation: https://docs.python.org/3.5/library/sys.html#sys.exit
used @michael-butscher solution, but your's is the same idea without the function. Thank you!
– Bobert
Nov 12 at 23:03
add a comment |
You can use
import sys
sys.exit(0)
to immediately stop executing further program statements, so something like
elif y == "quit":
import sys
sys.exit(0)
should work.
Documentation: https://docs.python.org/3.5/library/sys.html#sys.exit
You can use
import sys
sys.exit(0)
to immediately stop executing further program statements, so something like
elif y == "quit":
import sys
sys.exit(0)
should work.
Documentation: https://docs.python.org/3.5/library/sys.html#sys.exit
answered Nov 12 at 22:36
quant
1,59211526
1,59211526
used @michael-butscher solution, but your's is the same idea without the function. Thank you!
– Bobert
Nov 12 at 23:03
add a comment |
used @michael-butscher solution, but your's is the same idea without the function. Thank you!
– Bobert
Nov 12 at 23:03
used @michael-butscher solution, but your's is the same idea without the function. Thank you!
– Bobert
Nov 12 at 23:03
used @michael-butscher solution, but your's is the same idea without the function. Thank you!
– Bobert
Nov 12 at 23:03
add a comment |
I think that both writing a function and using sys.exit
are overkill for what OP asked, depending whether you're trying to break out of the loop or exit the program entirely
Specifically regarding your question, you can break
right after your input()
and it will exit the loop without executing the rest of the run. (BTW, you don't need to cast input to a string, input is a string by default)
y = input(": ")
if y.lower() == "quit":
break
if y == "fruits":
This breaks the current loop and reverts back to the previous loop. Useful because I wanted to create a "back" step at each loop, but doesn't end the whole program until you get to the first loop and enter "quit". Thank you for answering!
– Bobert
Nov 12 at 23:05
I suppose I misunderstood your question, the solution breaks out of thewhile
loop if put right after the input. Hope it was helpful anyway
– G. Anderson
Nov 12 at 23:16
No problem. It was helpful, as I also wanted a way to break the current loop without exiting the whole program.
– Bobert
Nov 12 at 23:26
add a comment |
I think that both writing a function and using sys.exit
are overkill for what OP asked, depending whether you're trying to break out of the loop or exit the program entirely
Specifically regarding your question, you can break
right after your input()
and it will exit the loop without executing the rest of the run. (BTW, you don't need to cast input to a string, input is a string by default)
y = input(": ")
if y.lower() == "quit":
break
if y == "fruits":
This breaks the current loop and reverts back to the previous loop. Useful because I wanted to create a "back" step at each loop, but doesn't end the whole program until you get to the first loop and enter "quit". Thank you for answering!
– Bobert
Nov 12 at 23:05
I suppose I misunderstood your question, the solution breaks out of thewhile
loop if put right after the input. Hope it was helpful anyway
– G. Anderson
Nov 12 at 23:16
No problem. It was helpful, as I also wanted a way to break the current loop without exiting the whole program.
– Bobert
Nov 12 at 23:26
add a comment |
I think that both writing a function and using sys.exit
are overkill for what OP asked, depending whether you're trying to break out of the loop or exit the program entirely
Specifically regarding your question, you can break
right after your input()
and it will exit the loop without executing the rest of the run. (BTW, you don't need to cast input to a string, input is a string by default)
y = input(": ")
if y.lower() == "quit":
break
if y == "fruits":
I think that both writing a function and using sys.exit
are overkill for what OP asked, depending whether you're trying to break out of the loop or exit the program entirely
Specifically regarding your question, you can break
right after your input()
and it will exit the loop without executing the rest of the run. (BTW, you don't need to cast input to a string, input is a string by default)
y = input(": ")
if y.lower() == "quit":
break
if y == "fruits":
answered Nov 12 at 22:41
G. Anderson
1,06929
1,06929
This breaks the current loop and reverts back to the previous loop. Useful because I wanted to create a "back" step at each loop, but doesn't end the whole program until you get to the first loop and enter "quit". Thank you for answering!
– Bobert
Nov 12 at 23:05
I suppose I misunderstood your question, the solution breaks out of thewhile
loop if put right after the input. Hope it was helpful anyway
– G. Anderson
Nov 12 at 23:16
No problem. It was helpful, as I also wanted a way to break the current loop without exiting the whole program.
– Bobert
Nov 12 at 23:26
add a comment |
This breaks the current loop and reverts back to the previous loop. Useful because I wanted to create a "back" step at each loop, but doesn't end the whole program until you get to the first loop and enter "quit". Thank you for answering!
– Bobert
Nov 12 at 23:05
I suppose I misunderstood your question, the solution breaks out of thewhile
loop if put right after the input. Hope it was helpful anyway
– G. Anderson
Nov 12 at 23:16
No problem. It was helpful, as I also wanted a way to break the current loop without exiting the whole program.
– Bobert
Nov 12 at 23:26
This breaks the current loop and reverts back to the previous loop. Useful because I wanted to create a "back" step at each loop, but doesn't end the whole program until you get to the first loop and enter "quit". Thank you for answering!
– Bobert
Nov 12 at 23:05
This breaks the current loop and reverts back to the previous loop. Useful because I wanted to create a "back" step at each loop, but doesn't end the whole program until you get to the first loop and enter "quit". Thank you for answering!
– Bobert
Nov 12 at 23:05
I suppose I misunderstood your question, the solution breaks out of the
while
loop if put right after the input. Hope it was helpful anyway– G. Anderson
Nov 12 at 23:16
I suppose I misunderstood your question, the solution breaks out of the
while
loop if put right after the input. Hope it was helpful anyway– G. Anderson
Nov 12 at 23:16
No problem. It was helpful, as I also wanted a way to break the current loop without exiting the whole program.
– Bobert
Nov 12 at 23:26
No problem. It was helpful, as I also wanted a way to break the current loop without exiting the whole program.
– Bobert
Nov 12 at 23:26
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%2f53271077%2fhow-to-end-program-if-input-quit-with-many-if-statements%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
You can create your own
myInput
function which callsinput
and callssys.exit(0)
if it founds "quit" and returns input otherwise. Then you replace allinput
s bymyInput
.– Michael Butscher
Nov 12 at 22:37