#๐Ÿ”’ Where is "else"?

77 messages ยท Page 1 of 1 (latest)

median egret
royal hullBOT
#

@median egret

Python help channel opened

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.

median egret
#

is_john = True

if is_john: True
print(f"You are {first_name}")
else:
print(f"You are not {first_name}")

heavy jacinth
#

well the code is wrong

tawdry ledge
#

indent

heavy jacinth
#

because you didnt put colon at the end and didnt indent the print

heavy jacinth
#

colon after 'true'

cunning pendant
#

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?

median egret
#

im following a guide

cunning pendant
#

Did you mean == True?

#

If you meant if is_john == True:, just put if is_john:.

median egret
#

this is what it did in the guide and i tried testing something out

royal hullBOT
#
Comparisons to `True` and `False`

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.

median egret
#

thanks

#

but

#

one more thing

#

why dont i have else

#

it doesnt show up

pastel gorge
#

what's before that part in the code?

median egret
cunning pendant
#

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.

median egret
#

wait this sounds kinda dumb but whats indent

cunning pendant
#

Adding spaces at the start of the line.

median egret
#

i js pressed enter

#

oh

heavy jacinth
#
if isjohn #isjohn == True:
  print(bla bla bla)
else:
  print()

cunning pendant
median egret
#

so this?

heavy jacinth
#

and indent print

median egret
heavy jacinth
#

yea

median egret
pastel gorge
#

so you do know if works with any expression that evaluates to a truthy value?

heavy jacinth
#

every if statement must end with ':;

#

i mean ':'

pastel gorge
#

so if is_john is already a boolean value, you don't need == True

heavy jacinth
#

a colon

heavy jacinth
#

if booleans are true you can just write if 'boolean':

median egret
#

okay

#

thanks

#

also i wrote it now

#

with the indents

#

and the colon

#

after is_john

heavy jacinth
#

same goes for while, for, if-elif-else statements

heavy jacinth
#

press 'tab' before the p in print

#

this is indenting

median egret
#

oh okay

#

mb

heavy jacinth
#

and remove the extra spaces

heavy jacinth
median egret
#

ah yes now theres

#

lines

#

on the left side

heavy jacinth
#

indents are just 4 spaces

median egret
#

and they go infront of only the print?

pastel gorge
#

here's the logic โ€“ after an if, you want to indent the block of code that should be run if the condition matches

median egret
#

oh okay

pastel gorge
#

!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")
royal hullBOT
median egret
#

so after if and else i indent

#

thanks, it works now

pastel gorge
#

the indentation makes all the difference

median egret
#

and else actually works now

median egret
#

thanks alot guys

pastel gorge
#

you can do !close if you have no further questions

royal hullBOT
#
Python help channel closed for inactivity

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.