#🔒 Help with adding conditions to if and elif statments

79 messages · Page 1 of 1 (latest)

fathom garnet
#

Essentially i'm wondering how to add more conditions into an elif or if statment. I'm very new at this, so sorry if it's really simple.

low groveBOT
#

@fathom garnet

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.

paper quest
fathom garnet
paper quest
#

The error being..?

fathom garnet
paper quest
#

Thats the stuff you’ll be fixing with the indentation.

#

So that means that what you tried to fix the indent, wasnt the right way.

#

That line needs to start on the same indent as the line above it.

fathom garnet
#

so i am correct on the fact you can add an if inside an elif or no?

paper quest
#

Yes for sure you can 🙂

fathom garnet
#

see whenever i edit any code the error comes back

#

adding indents for example

paper quest
#

!e
x = 5
if x > 1:
if x < 10:
print("x between 1 and 10")

low groveBOT
#

@paper quest :white_check_mark: Your 3.12 eval job has completed with return code 0.

x between 1 and 10
paper quest
#

Ifbyou show line numbers i can guide you better but:

fathom garnet
#

this is so weird

#

im adjusting both indents

#

and it keeps having the same red line..?

paper quest
#
any line of code
if abc: # needs to start on same level as line above
    Do things # needs to be indented 4 spaces
#

The problem is where the if’s start.

fathom garnet
#

this looks correct yes?

paper quest
#

No

fathom garnet
#

ik the error at exit but im talking about indentation

paper quest
#

Line 38 is indented. But it shouldnt be

#

Line 37 is indented with the incorrect amount of spaces (although that often doesnt error anymore)

fathom garnet
#

yeah but whenever i dont indent it it will error because the variable "decision" is declared on the other if

paper quest
#

ifs dont have scope.

fathom garnet
paper quest
#

Line 38, needs to start on the same level as line 37

fathom garnet
#

hm ok im close now only one if is erroring

paper quest
#

Good good!

fathom garnet
#

ahb yes

#

wait

#

like thus?

paper quest
#

Let me show you this message we wrote for the bot.

#

!indent

low groveBOT
#
Indentation

Indentation is leading whitespace (spaces and tabs) at the beginning of a line of code. In the case of Python, they are used to determine the grouping of statements.

Spaces should be preferred over tabs. To be clear, this is in reference to the character itself, not the keys on a keyboard. Your editor/IDE should be configured to insert spaces when the TAB key is pressed. The amount of spaces should be a multiple of 4, except optionally in the case of continuation lines.

Example

def foo():
    bar = 'baz'  # indented one level
    if bar == 'baz':
        print('ham')  # indented two levels
    return bar  # indented one level

The first line is not indented. The next two lines are indented to be inside of the function definition. They will only run when the function is called. The fourth line is indented to be inside the if statement, and will only run if the if statement evaluates to True. The fifth and last line is like the 2nd and 3rd and will always run when the function is called. It effectively closes the if statement above as no more lines can be inside the if statement below that line.

Indentation is used after:
1. Compound statements (eg. if, while, for, try, with, def, class, and their counterparts)
2. Continuation lines

More Info
1. Indentation style guide
2. Tabs or Spaces?
3. Official docs on indentation

paper quest
fathom garnet
#

am i doing somthign wrong in the general function of the code?

paper quest
#

Just the decision.upper is missing parenthesis

fathom garnet
#

yeah

#

thast why its not working

#

arent these...?

#

parenthesis

fathom garnet
paper quest
#

Thise are quotes no?

#

()

#

Maybe im wrong, idk lol

#

Sorry I gotta run, work work

fathom garnet
#

no your right

#

i just had a brain fart

#

ok thanks for helping

#

lmao

paper quest
paper quest
fathom garnet
#

wut

#

in english

#

ohhh

#

ok thanks

paper quest
#

Remember calling a function / method is done with ()

fathom garnet
#

like this?

paper quest
#

print <- the function print
print() the call to do function print

paper quest
#

!e
hello = "world"
print(hello.upper())

low groveBOT
#

@paper quest :white_check_mark: Your 3.12 eval job has completed with return code 0.

WORLD
fathom garnet
#

oh shoot

#

yeah

#

ok thanks!

paper quest
low groveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.