Ask coding questions

← Back to all posts
Does anyone know how to make python auto stop if you put something in?
SnowpawValdez

This is my code.
grocery_list =["ham","carrots","apples","chips","bread","milk","candy"]
print("welcome to your virtaul grocery list!")
print("The first item on the list is ham")
print("Here's the list.")
print(grocery_list)
question = input("Do you have ham? ")
if (question =='yes'):print("ok!")
elif (question == 'no'):print("cmon,get it! ok,NOW we can continue.")
else :
print("THE SYSTE<SM HAAAS>> NSOWE BE))(N BRO(^$#KEN.") right here I need to tell it to stop printing here if they say something other than yes or no.

Answered by OldWizard209 [earned 5 cycles]
View Answer
Voters
SnowpawValdez
Comments
hotnewtop
OldWizard209

Easy. Just import the sys module at the top, and wherever you want to stop, just do sys.exit():

SnowpawValdez

@OldWizard209 Thank you! I'll try that.

SnowpawValdez

@SnowpawValdez Thanks! I removed "sys." and changed it to exit () and it's now working!

InvisibleOne

Yeah
exit()
You don't need to import sys, but you can if you want.

SnowpawValdez

@InvisibleOne So the command is exit()? I should be able to just type that?

SnowpawValdez

I'm new with coding python..😬

SnowpawValdez

@InvisibleOne Got it, Thanks!

InvisibleOne

yes, just typing exit() will make the code stop if it hits that point. @SnowpawValdez

InvisibleOne

I'm not sure why people use sys.exit() since they both do the same thing, maybe that was a python2 habit that they carried through into python3.