#๐ Where is "else"?
77 messages ยท Page 1 of 1 (latest)
@median egret
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
is_john = True
if is_john: True
print(f"You are {first_name}")
else:
print(f"You are not {first_name}")
well the code is wrong
indent
because you didnt put colon at the end and didnt indent the print
colon behind : ?
colon after 'true'
It's not that there's no colon, it's that the True is confusing things. Get rid of the true. Why do you have that there?
im following a guide
this is what it did in the guide and i tried testing something out
okay
It's tempting to think that if statements always need a comparison operator like == or !=, but this isn't true.
If you're just checking if a value is truthy or falsey, you don't need == True or == False.
# instead of this...
if user_input.startswith('y') == True:
my_func(user_input)
# ...write this
if user_input.startswith('y'):
my_func(user_input)
# for false conditions, instead of this...
if user_input.startswith('y') == False:
my_func(user_input)
# ...just use `not`
if not user_input.startswith('y'):
my_func(user_input)
This also applies to expressions that use is True or is False.
what's before that part in the code?
You also need to indent the lines after if and else. Idk if that's why the plugin isn't suggesting else, but it's wrong currently.
Adding spaces at the start of the line.
if isjohn #isjohn == True:
print(bla bla bla)
else:
print()
But with a colon at the end of th first line.
so this?
and indent print
colon after true?
yea
okay
so you do know if works with any expression that evaluates to a truthy value?
so if is_john is already a boolean value, you don't need == True
a colon
oh okay
yes but for the OP convenience i put it to make them understand
if booleans are true you can just write if 'boolean':
okay
thanks
also i wrote it now
with the indents
and the colon
after is_john
but
same goes for while, for, if-elif-else statements
indent the print statements
press 'tab' before the p in print
this is indenting
and remove the extra spaces
or just add 4 spaces
refer to this
indents are just 4 spaces
and they go infront of only the print?
here's the logic โ after an if, you want to indent the block of code that should be run if the condition matches
oh okay
!e
variable = True
if variable:
print("variable is true")
print("this is always printed")
variable = False
if variable:
print("variable is true")
print("this is always printed")
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | variable is true
002 | this is always printed
003 | this is always printed
the indentation makes all the difference
and else actually works now
you can do !close if you have no further questions
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.