Question: how I use exit function in if else
Repl link:
code snippet
Question: how I use exit function in if else
Repl link:
code snippet
Hi @VishalSaresa, welcome to the community!
Here is how to write an if statement:
if (condition):
# do something
else:
# do something else
You can replace (condition)
with yours and replace the comments with exit()
If you need more help then please send a link to your Repl and/or send a code snippet. Check out this guide showing you how:
Hello, welcome to the community @VishalSaresa if your question hasn’t been answered yet or you don’t understand I can try to help you here.
Example:
ex1 = input('Is your name jerry?')
if ex1.lower() == 'yes':
print('You are the best')
elif ex1.lower() == 'no':
print('Ohh, ok I guess thats ok')
else:
exit()
A bit more helpful:
ex1 = input('Is your name jerry?')
Asking them a question
if ex1.lower() == 'yes':
print('You are the best')
If they say yes repeat there the best
elif ex1.lower() == 'no':
print('Ohh, ok I guess thats ok')
Otherwise, it will say that its ok
else:
exit()
Now the exit statement, the exit statement could be put in any other area you want it just to end what’s going on.
Note + Replit Link:
Link: https://replit.com/@Chandler0Bing/VishalSaresa-Help
In that link, you are able to run it and you are able to see it and do whatever you need and try to figure it out. The exit()
statement is usually used when there are many ELIF or IF statements. Like if someone is trying to leave things if it asks would you like to leave and they say yes then it will go to the exit()
statement to end it and let them leave. Theres a lot of ways to use it.
He wants else if
Python has a command called elif
, which means else if, which goes after an if, so if if is not right, then it will do the code in the elif
if it matches the conditional too.
do this:
if {put condition here}:
#some code
elif {put condition here}
exit()
I’m pretty sure that exit should always work
That indentation, python is not going to be too happy about it if you continue it like that
Ok, whatever. Typo. My bad.
OP never specified so you don’t know. OP just said “if else.”
Hi @VishalSaresa,
You have made an indentation error…
IndentationError
Here, after line 4, you have lines 5 and 6 in the if block. But they are not equally indented when they should have been (should be on the same indentation level.)
Line 6 with exit()
has only 1 space for indentation when 1 indentation level = 2 or 4 spaces. So just add an extra space in front of the exit()
if name == "ben":
print("You're not welcome here Evil ben ! Get out !!")
exit()
else :
print ("Hello" + " Thank you so much for coming in today \n")
menu = "Black coffee , Espresso , latte , cappucino "
Here you can see that print and exit are on the same level.
If you still get the error then you have probably mixed spaces with tabs. So just replace all the tab characters with spaces.