r/learnpython • u/Michalreddit • Sep 02 '14
Dont upvote just help if you know please
It's my second day of learning programming and I have been using a book called "How to think like a computer scientist - Learning with Python 3" currently finishing chapter 2 and I'm stuck at the last assignment.
It is meant to say what day of the week im leaving on. Monday = first, Tuesday = second etc.
I can't figure out why It always answers "That is a first day of week" no matter of whether I type monday, tuesday or any other day.
I guess it's something stupid but I cant figure it out. Anyone sees the bug?
2
u/gbourdin Sep 04 '14
Your question has already been answered but since you pointed out that you're learning the language as well as learning to program, I figured out I'd throw in an example that solves this too and uses some python magic and some common concepts, see if you can figure it out:
1
18
u/RubyPinch PEP Shill Sep 02 '14
if g == "monday" or "on monday" or "ON MONDAY" or "on Monday"ends up being
if (g == "monday") or ("on monday") or ("ON MONDAY") or ("on Monday")and strings are true when they are not empty
so (g == "monday") is false, but ("on monday") is a non-empty string, which is true
15
u/herminator Sep 02 '14
Others have already pointed out what you did wrong, but here's another option you might consider:
if "monday" in g.lower():
This will be true for anything that contains the word monday in any form. E.g:
>>> answer = "I'm going to leave next Monday, I think"
>>> "monday" in answer.lower()
True
>>> "tuesday" in answer.lower()
False14
Sep 03 '14
Ah, my favourite question...
http://www.reddit.com/r/learnpython/comments/2fangd/dont_upvote_just_help_if_you_know_please/ http://www.reddit.com/r/learnpython/comments/18m0ou/whats_wrong_with_my_if_statement/ http://www.reddit.com/r/learnpython/comments/23frac/or_in_if_commands/ http://www.reddit.com/r/learnpython/comments/1z7j2u/if_statements_and_strings/ http://www.reddit.com/r/learnpython/comments/2bt7ah/functionif_statement_wont_work_the_way_i_want_it/ http://www.reddit.com/r/learnpython/comments/1gws15/anybody_know_why_my_code_will_not_read_the_second/ http://www.reddit.com/r/learnpython/comments/1eghto/so_can_you_not_use_and_or_or_in_an_if_statement/ http://www.reddit.com/r/learnpython/comments/1h0hlb/very_beginner_question_can_somebody_explain_why/ http://www.reddit.com/r/learnpython/comments/1lqbx7/elif_command_not_producing_desired_effect_in/ http://www.reddit.com/r/learnpython/comments/2bu4ch/alright_i_give_up_help_please/ http://www.reddit.com/r/learnpython/comments/tnp5l/not_sure_how_to_set_up_the_if_else_to_get_this_to/
The mods should put a big warning in the sidebar saying "CHANGE if x == y or z TO if x == y or x == z." Seriously.
3
u/zahlman zahlman Sep 03 '14
Seriously, I don't understand how this question is so overwhelmingly common in the Python community, seems to have become more common in the last... less than a year, and doesn't seem to have ever been nearly as common in communities I've belonged to discussing other programming languages (that exhibit the same behaviour).
8
u/stevarino Sep 03 '14
Python is a great language to learn with (download installer, open idle, not to mention all the free resources), and because of this, more people are learning outside of traditional environments such as school or work.
I think this is great, personally. I remember my intro to programming class in C++ spent an entire day on how to write a logical expression, and we still messed it up. Learning to program is a great life skill for almost anyone, so I hope we can all be patient and understanding.
/soapbox
3
u/zahlman zahlman Sep 03 '14 edited Sep 03 '14
Oh, I'm definitely on board with what you're saying. I'm just wondering if there's a gap in our educational materials somewhere.
Edit: Or maybe, when people have the equivalent problem in C, it doesn't make it to the Internet for some reason?
3
u/joyeusenoelle Sep 03 '14
I wonder how much it's related to Python's more "natural" syntax.
||obviously can't be used in a sentence the way English "or" can, but becauseoris an English word, maybe novice programmers feel like they can use it the way they do in English - "If it's thisorthatorthe other thing, do X".Not sure why that would have become more common in the last year, though!
3
2
Sep 03 '14
This describes me.. I'm just a hobbyist programmer. I used to program years ago in QBASIC and now I'm learning Python and I love the helpful community that is here on /r/learnpython.
3
u/newbie12q Sep 03 '14
I think the reason is since python is more natural(meaning more close to human language) then other languages , people tend to use natural language as a substitute for the real syntax, though most of the time it works at some cases it fails.
4
Sep 02 '14
if g == "monday" or "on monday" or "ON MONDAY" or "on Monday"
should be
if g == "monday" or g == "on monday" or g == "ON MONDAY" or g == "on Monday":
or
if g in ["monday", "on monday", "ON MONDAY", "on Monday"]:
3
u/turkeyfied Sep 02 '14
Fix your if statements. You need to have
foo == "bar" or foo == "baz"
rather than what you have
4
u/hharison hsharrison Sep 02 '14
or
foo in ('bar', 'baz')2
u/turkeyfied Sep 03 '14
Indeed. I'm only a casual Python programmer (more a C#/Java kinda guy), so I always forget that one.
3
2
3
u/Michalreddit Sep 02 '14
Thanks guys all of you!