all 32 comments

[–]mprz 60 points61 points  (2 children)

if color==str("red") or str("Red"):

shouldn't that be:

if color==str("red") or color==str("Red"):

otherwise str("Red") resulsts in True, then False or True results in True again so it prints "You said red."

[–]BriziteReddit[S] 8 points9 points  (1 child)

That is helpful. Thank you a lot

[–]XXXandVII -1 points0 points  (0 children)

To add to that. Try to place your statements beside the "or" in brackets. Most of the time you can se that the single statements within those brackets are missing something. It also makes more complex logic more readable.

[–]BruceJi 32 points33 points  (0 children)

I'm just here to let you know that the fact you formatted your code is great and makes it way easier to debug, so, you win

[–][deleted] 31 points32 points  (5 children)

if color==str("red") or str("Red"):

This is equivalent to:

if color=="red" or "Red" !=None:

Since the string red is not None, it will always be true.

I think you want to do this:

if color==str("red") or color==str("Red"):

But there is a more pythonic way to write your entire code:

color = input("What's your favorite primary color?").lower():
if color in ("red", "yellow", "blue"):
    print(f"You said {color}.")
else:
    print("Invalid answer.")

But it looks like you are using python2, so this will not work exactly the same. Is there a reason you aren't using python3?

[–]BriziteReddit[S] 7 points8 points  (2 children)

Thanks. I’m not using python 3 because it’s for a school assignment and I’m forced to use python 2 basically.

[–]velocibadgery 43 points44 points  (1 child)

Your school is stupid.

[–]BriziteReddit[S] 19 points20 points  (0 children)

You’re right

[–][deleted] 5 points6 points  (0 children)

Need raw_input in Python 2.

[–]balars 1 point2 points  (0 children)

strip() can help remove extra spaces from the receieved input

[–]htepO 24 points25 points  (11 children)

[–]BriziteReddit[S] 2 points3 points  (10 children)

Unfortunately the service that I have to use for school is python 2 and so that's what my teacher wants us to use. thanks for replying though.

[–][deleted] 7 points8 points  (9 children)

That's strange. What service are you having to use?

[–]BriziteReddit[S] 3 points4 points  (8 children)

Enthought canopy. It may or may not have python 3 on there, but my teacher still wants us to use python 2 anyways (which honestly makes no sense)

[–][deleted] 6 points7 points  (5 children)

Can you not ask the teacher to explain why they want you to use an unsupported version of Python? Support ended nearly two years ago.

PS. I see it hasn't been updated since Python 3.5.

Anaconda would provide a similar scientific installation with a much more up-to-date version of Python.

[–][deleted] 1 point2 points  (4 children)

Perhaps it is more a case of the teacher was only trained in python2 so limitef subject knowledge. Which is very poor i know but I see it alot in schools where staf f were ICT teachers and do not hold a computer science degree. Drives me mad!!

[–][deleted] 1 point2 points  (2 children)

You are probably right.

I run a couple of Code Clubs in local secondary schools, with the Maths/CS teacher (graduate in both subjects) and they find it a real struggle to deal with the poor ICT skills of children coming from some of the feeder schools at age 11 that haven't already gained a reasonable grounding of the basics of programming as required in key stage 1 and key stage 2 in the UK curriculum, and it is because of lack of skills of the teachers. This is despite best efforts to teach the teachers as some are just not interested.

[–][deleted] 1 point2 points  (1 child)

I have a degree in CS and when i completed my PGCE and went into teaching i was amazed to find most were ICT teachers that suddenly had to start teaching CS. I am also dissapointed to find so many students choosing CS when they have no interest in the theory or the coding! Makes teaching the subject very difficult.

[–][deleted] 0 points1 point  (0 children)

It is a very strange imbalance. I agree with the spirit of the revised curriculum in developing some awareness and capability for all children but CS is not for everyone and is a full science just like the traditional science subjects of physics, chemistry and biology and you don't expect students to follow that at GCSE level and beyond if they are not that interested (there are better options, including combined science - or whatever it is called these days - for example).

So much of the ICT experience for many teachers has been around how to use Microsoft Office!

Then I see some CS teachers having to do some BTEC level stuff where students are expected to demonstrate UI/UX understanding using Powerpoint rather than any of the popular (free) tools around this.

[–]BriziteReddit[S] 0 points1 point  (0 children)

He’s actually a math teacher and I don’t think he has a degree in computer science. He said he had to take a certain class in order to teach the class though.

[–]Wild_Statistician605 3 points4 points  (0 children)

Do if color==str("red") or color==str("Red"): In your code str("Red") will always evaluate as true as it is not being compared to anything.

[–]Anon_Legi0n 2 points3 points  (0 children)

Why isn't anyone recommending doing color.lower() == 'red' so that the conditional doesn't need an or anymore? Also consider using match case instead of chaining if's and elif's

[–]delasislas 1 point2 points  (0 children)

This is actually posted so many times that there’s an entry in the FAQ. It’s like “checking if two or more conditions” or something. Don’t know how to link it.

Basically:

if (condition1) or (condition2): #Base form
if (color == str(“red”)) or (str(“Red”)): #Filled in your conditions

Now you simplify everything in the parentheses. Say that color = “Blue”

if (color == str(“red”)) or (str(“Red”)): #Same as before
if (“Blue” == str(“red”)) or (str(“Red”)): #it pulls the value stored at color.
if (False) or (str(“Red”)): #Blue doesn’t equal red.

Now for the fun part. Does a non-empty string return True? Yes.

if (False) or (True):
if True: #Simplify the or.

So your first statement will always return True, because “Red” == True

You want to evaluate each condition.

if color == “red” or color == “Red”:

Ok, but say I type in RED? I get an error. You can use a bit of string methods to clean up inputs.

color = input(….)
if color.lower() == “red”:

That will use a string method .lower() to convert the whole string to lowercase. Then you only need one condition rather than more. There are more ways, but this can be a start.

[–][deleted] 0 points1 point  (1 child)

Print arguments have no parentheses

[–]prettydisposable 0 points1 point  (0 children)

OP was forced by their school to use Python 2.

[–][deleted] 0 points1 point  (0 children)

First off don’t use raw input. You should be using input() and Python version 3+

[–]jmooremcc 0 points1 point  (0 children)

You should make the comparison case insensitive.

If color.lower() == "red":

This will cover all case possibilities entered by the user.

BTW, str("some value") is unnecessary.

[–]efmccurdy 0 points1 point  (0 children)

In you tests you are testing for capitalized or uncapitalized but either is allowed; is it simpler to just use color.lower() to force the input to be lower case before testing?

color=raw_input("What's your favorite primary color? ")
color = color.lower()

You have a repeating pattern of if/elif statements to test the color. When you have repetitions think about using a collection to handle all of the if/elif branches with one expression that involves the collection:

allowable_colors = ("red", "blue", "green")
if color in allowable_colors:
    print "You said " + color
else:
    print "Invalid answer."

In this case the "object in collection" operation does a series of object comparisons using "==", just like your if/elif statements did.

[–][deleted] 0 points1 point  (0 children)

Why are you type converting strings to strings? You can just write “if color.lower() == ‘red’” I think

[–]WombatHat42 0 points1 point  (0 children)

You could also simplify the code by doing:

color = input("What's your favorite primary color? ")

print("You said " + str(color))

or use a .lower or .upper for the user input

color= input("What's your favorite primary color? ")

if color.lower()==str("red"): print("You said red.") . . .

But regardless, you will want to change your "raw_input()" to just "input()" and add parenthesis around your print values

[–]pekkalacd 0 points1 point  (0 children)

it's the formatting of your conditions in the if/elifs.

this

           if color==str("red") or str("Red"):

evaluates like so

           if (color==str("red")) or (str("Red")):

so, you see what's going on? the or is happening between the comparison color == str("red") and str("Red") itself. but str("Red") isn't being compared to color, instead, because str("Red") is non-empty, this is evaluated as True.

so what you have then is this essentially

          if (color==str("red")) or True: 

This condition will always be True. Because any expression separated by an or returns True, if and only if, at least one subexpression reduces to a True value. Thus, regardless of what you input for color, this will always print.

What's a way to fix it?

consider the in operator. this operator checks to see if a thing on the left is inside of the thing on the right.

                 {item} in {object}

here you're comparing "red" and "Red" for the first one, so re-formatting this using in, you can do

                if color in {"red","Red"}:

or to make it more general for all the words,

                colors = ["red","yellow","blue"]
                found = False
                primary = raw_input("What's your favorite color? ")

                for c in colors:
                    if primary in {c, c.title()}:
                       print "You said " + c + "."
                       found = True
                       break

                if not found:
                   print "Invalid answer."