r/learnpython • • Sep 02 '14

Dont upvote just help if you know please

http://pastebin.com/CHWhwNNB

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?

6 Upvotes

21 comments sorted by

3

u/Michalreddit Sep 02 '14

Thanks guys all of you!

3

u/loveandkindness Sep 02 '14

Keep it up! Good luck!

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:

http://pastebin.com/77Fr1iF3

1

u/Michalreddit Sep 04 '14

thanks that looks much smarter.

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()
False

14

u/[deleted] Sep 03 '14

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 because or is an English word, maybe novice programmers feel like they can use it the way they do in English - "If it's this or that or the other thing, do X".

Not sure why that would have become more common in the last year, though!

3

u/zahlman zahlman Sep 03 '14

Maybe I've just been paying more attention.

2

u/[deleted] 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

u/[deleted] 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

u/Spaztazim Sep 02 '14

Also on your Sunday elif, you put on Saturday as the last one.

2

u/Exodus111 Sep 03 '14

Classic mistake.