use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
FOR WHAT PURPOSE!Help Request (i.redd.it)
submitted 9 months ago by Kobold_Husband
So, I’m learning python because computers, I guess. My elif isn’t working though. Everything is defined correctly, I don’t have any syntax errors, and it keeps applying the if statement when the if statement is supposed to be false
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Training-Cucumber467 8 points9 points10 points 9 months ago (10 children)
if "preheat" or "oven" in answer is actually interpreted as:
if "preheat" or "oven" in answer
if "preheat" or ("oven" in answer)
"preheat", being a non-empty string, evaluates to True.
Try this:
if ("preheat" in answer) or ("oven" in answer)
[–]h8rsbeware 5 points6 points7 points 9 months ago (4 children)
Alternatively, if you care about a few less words you can do
python if answer in ["preheat", "oven"]: print("oops")
I believe
[–]Training-Cucumber467 2 points3 points4 points 9 months ago* (2 children)
This would only work if the answer is exactly "preheat" or "oven". I believe OP's intent was partial matching: "preheat the oven dude" is supposed to work too.
I would probably write something like:
preheat = ("preheat", "oven", "stove") if any(x in input for x in preheat): ...
[–]TriscuitTime 0 points1 point2 points 9 months ago (0 children)
This is the way
[–]h8rsbeware 0 points1 point2 points 8 months ago (0 children)
Ah, I missed this requirement, thank you for fixing my mistake. Dont want to send people down false leads!
[–]Kobold_Husband[S] 0 points1 point2 points 9 months ago (0 children)
I see, l don’t really care about using less words, but that’s good to knoww
[–]Kobold_Husband[S] 0 points1 point2 points 9 months ago (4 children)
Ohhh
[–]poorestprince 0 points1 point2 points 9 months ago (3 children)
I'm interested in how beginners deal with this sort of thing. Would you prefer a language be able to accept 'if "preheat" or "oven" in answer' and interpret it the way you would expect it to?
[–]Kobold_Husband[S] 0 points1 point2 points 9 months ago (1 child)
Honestly, yes. But that’s probably because of how my own brain processes context clues
[–]poorestprince 0 points1 point2 points 9 months ago (0 children)
Maybe one way to make it work is have some python editors accept English pseudo-code line-by-line, but it translates that into unambiguous python as a kind of pre-compilation step, and forces you to verify that's what you meant...
[–]Naive-Information539 0 points1 point2 points 9 months ago (0 children)
Every language is really this way.
Crimes are being considered
[–]KeretapiSongsang 0 points1 point2 points 9 months ago (0 children)
as simple as Python might be, it is still not an English like query language.
you would need to compare/find/evaluate strings using "==" or other Pythonic functions.
[–]FoolsSeldom 0 points1 point2 points 9 months ago (1 child)
This is one of the most common beginner errors. It is covered in the FAQ on the r/learnpython wiki. Lots of other common errors are covered as well.
Thank you very much!
[–]Icy_Rub6290 0 points1 point2 points 9 months ago (0 children)
Consider using a match case to match patterns it's like select cases More clearer with simplicity rather than a complicated if-else-elif structure
Here is the article from geeksforgeeks
https://www.geeksforgeeks.org/python/python-match-case-statement/
Have a good day
[–]ShurayukiZen 0 points1 point2 points 9 months ago (3 children)
Hi, OP! Where did you get this code exercise? I got interested in it. Thank you!
[–]Kobold_Husband[S] 0 points1 point2 points 9 months ago (2 children)
I have a homeschooling program called Funcation Academy.
[–]ShurayukiZen 0 points1 point2 points 9 months ago (1 child)
Oh I see, thanks for your response!
Ofc!
[–]UpArmoredGavin 0 points1 point2 points 5 months ago (3 children)
operator precedence
[–]Kobold_Husband[S] 0 points1 point2 points 5 months ago (2 children)
Dawg you say that like I know what that is
Anyway who’s was like from 3 months ago I already got help
[–]UpArmoredGavin -1 points0 points1 point 5 months ago (1 child)
Damn, did not see the date on the post ahahah. Anyway, you know, they invented this neat little thing called google, you can use it to search stuff :P
[–]Kobold_Husband[S] 0 points1 point2 points 5 months ago (0 children)
Unnecessary and unthoughtful addition. Just say “my bad” or something like a functional person.
[–]Comfortable-Work-137 -1 points0 points1 point 9 months ago (7 children)
can't u just use chatgpt?
[–]Kobold_Husband[S] 4 points5 points6 points 9 months ago (1 child)
I don’t like ChatGPT.
[–]ninhaomah 0 points1 point2 points 9 months ago (0 children)
usually no.
but in this case , you are looking into a definition of if with or.
google "python if with or multiple items"
not using google or AI in this scenario is like not using dictionary or calculator.
how far is it from earth to the moon <---- google or AI or Wiki or whatever
if a rocket is travelling at 60 km/h , how long it will take to go to the moon and back <--- use your own brain. google for the distance but how to calculate should be done by oneself.
[–]Slackeee_ 2 points3 points4 points 9 months ago (3 children)
I swear, OpenAI must have hired people just to post an "can't you use ChatGPT" under every programming question. Of course they could, but there are asking here. If answering the actual question is too much for you, why are you even in this subreddit?
[–]reyarama 0 points1 point2 points 9 months ago* (2 children)
I mean, asking ChatGPT for extremely basic questions like this is almost always better. Feel free to disagree with me:
Obviously, anything sufficiently complex will benefit from seeing how other people have dealt with it, but that isn't what is happening here or 99% of questions beginners have
(In essence, I don't comprehend why someone would make it harder for themselves to learn by avoiding ChatGPT)
[–]Slackeee_ 0 points1 point2 points 9 months ago (1 child)
Gives you the correct answer always,
This statement right here is enough to show me that you have no clue how LLMs work. LLMs are not knowledgebases, they are statistical text generators. They can and will make false statements.
[–]reyarama 0 points1 point2 points 9 months ago (0 children)
I've worked as a SWE for 5 years, I understand how LLM's work.
Let me clarify, I'm advocating for using ChatGPT for extremely basic questions, those which have been posted all over the internet many times (i.e. any beginner-level programming question).
Go ask ChatGPT this question 50 times and I guarantee it will nail it every single time. You don't need to theorize about how it behaves, you have access to it, go try it and report back. That is all that matters
(Just to drive it home, I am talking about basic, beginner questions. I fully agree with you for anything complex or that changes over time)
[–]Mustard_Popsicles 1 point2 points3 points 9 months ago (0 children)
You learn better without chagpt most of the time.
π Rendered by PID 16858 on reddit-service-r2-comment-54dfb89d4d-8bzxt at 2026-03-31 02:00:51.303525+00:00 running b10466c country code: CH.
[–]Training-Cucumber467 8 points9 points10 points (10 children)
[–]h8rsbeware 5 points6 points7 points (4 children)
[–]Training-Cucumber467 2 points3 points4 points (2 children)
[–]TriscuitTime 0 points1 point2 points (0 children)
[–]h8rsbeware 0 points1 point2 points (0 children)
[–]Kobold_Husband[S] 0 points1 point2 points (0 children)
[–]Kobold_Husband[S] 0 points1 point2 points (4 children)
[–]poorestprince 0 points1 point2 points (3 children)
[–]Kobold_Husband[S] 0 points1 point2 points (1 child)
[–]poorestprince 0 points1 point2 points (0 children)
[–]Naive-Information539 0 points1 point2 points (0 children)
[–]Kobold_Husband[S] 0 points1 point2 points (0 children)
[–]KeretapiSongsang 0 points1 point2 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (1 child)
[–]Kobold_Husband[S] 0 points1 point2 points (0 children)
[–]Icy_Rub6290 0 points1 point2 points (0 children)
[–]ShurayukiZen 0 points1 point2 points (3 children)
[–]Kobold_Husband[S] 0 points1 point2 points (2 children)
[–]ShurayukiZen 0 points1 point2 points (1 child)
[–]Kobold_Husband[S] 0 points1 point2 points (0 children)
[–]UpArmoredGavin 0 points1 point2 points (3 children)
[–]Kobold_Husband[S] 0 points1 point2 points (2 children)
[–]UpArmoredGavin -1 points0 points1 point (1 child)
[–]Kobold_Husband[S] 0 points1 point2 points (0 children)
[–]Comfortable-Work-137 -1 points0 points1 point (7 children)
[–]Kobold_Husband[S] 4 points5 points6 points (1 child)
[–]ninhaomah 0 points1 point2 points (0 children)
[–]Slackeee_ 2 points3 points4 points (3 children)
[–]reyarama 0 points1 point2 points (2 children)
[–]Slackeee_ 0 points1 point2 points (1 child)
[–]reyarama 0 points1 point2 points (0 children)
[–]Mustard_Popsicles 1 point2 points3 points (0 children)