How to end program if input == “quit” with many if statements?












0














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!")









share|improve this question


















  • 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 inputs by myInput.
    – Michael Butscher
    Nov 12 at 22:37


















0














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!")









share|improve this question


















  • 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 inputs by myInput.
    – Michael Butscher
    Nov 12 at 22:37
















0












0








0







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!")









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 22:34









Bobert

284




284








  • 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 inputs by myInput.
    – Michael Butscher
    Nov 12 at 22:37
















  • 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 inputs by myInput.
    – 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 inputs 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 inputs by myInput.
– Michael Butscher
Nov 12 at 22:37














3 Answers
3






active

oldest

votes


















2














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)





share|improve this answer























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



















1














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






share|improve this answer





















  • used @michael-butscher solution, but your's is the same idea without the function. Thank you!
    – Bobert
    Nov 12 at 23:03



















0














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





share|improve this answer





















  • 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










  • 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











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









2














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)





share|improve this answer























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
















2














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)





share|improve this answer























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














2












2








2






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)





share|improve this answer














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)






share|improve this answer














share|improve this answer



share|improve this answer








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


















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













1














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






share|improve this answer





















  • used @michael-butscher solution, but your's is the same idea without the function. Thank you!
    – Bobert
    Nov 12 at 23:03
















1














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






share|improve this answer





















  • used @michael-butscher solution, but your's is the same idea without the function. Thank you!
    – Bobert
    Nov 12 at 23:03














1












1








1






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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











0














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





share|improve this answer





















  • 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










  • 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
















0














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





share|improve this answer





















  • 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










  • 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














0












0








0






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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










  • 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


















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%2f53271077%2fhow-to-end-program-if-input-quit-with-many-if-statements%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

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?