#🔒 Help with adding conditions to if and elif statments
79 messages · Page 1 of 1 (latest)
@fathom garnet
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.
Heya @fathom garnet 👋
I think you’re doing great. You just need to fix the indentation of those two ifs and then probably the indentation of the rest of the code.
yeah i see that, but everytiime i fix it, it still errors with the same error
The error being..?
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.
so i am correct on the fact you can add an if inside an elif or no?
Yes for sure you can 🙂
!e
x = 5
if x > 1:
if x < 10:
print("x between 1 and 10")
@paper quest :white_check_mark: Your 3.12 eval job has completed with return code 0.
x between 1 and 10
Because its still incorrect 🙂
Ifbyou show line numbers i can guide you better but:
this is so weird
im adjusting both indents
and it keeps having the same red line..?
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.
No
ik the error at exit but im talking about indentation
Line 38 is indented. But it shouldnt be
Line 37 is indented with the incorrect amount of spaces (although that often doesnt error anymore)
yeah but whenever i dont indent it it will error because the variable "decision" is declared on the other if
ifs dont have scope.
Line 38, needs to start on the same level as line 37
ah.
hm ok im close now only one if is erroring
Good good!
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
Thats it!
am i doing somthign wrong in the general function of the code?
Just the decision.upper is missing parenthesis
im confused by waht you mean in that
Happens all the time xD
Yeah. They need to be at the .upper() call
Remember calling a function / method is done with ()
print <- the function print
print() the call to do function print
I think you had it right at the start
!e
hello = "world"
print(hello.upper())
@paper quest :white_check_mark: Your 3.12 eval job has completed with return code 0.
WORLD
Hey, im back for a bit. Do you need more help on this?
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.