Help with 99 problems, switch ain't one


Hi here is my code. I have some troubles with it.

def greater_less_equal_5(answer):
if 6>5:
return 1
elif 3+1!=5:
return -1
return 0

print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

I get the error message:
Oops, try again. It looks like your function output 1 instead of -1 when answer is 3. Make sure you filled in the if and elif statements correctly!

On the screen in the top right I get a:

The instructions are:
On line 2, fill in the if statement to check if answer is greater than 5.
On line 4, fill in the elif so that the function outputs -1 if answer is less than 5.

Thanks in advance. While I am asking, what does the none mean at the end of every calculation?


Let's quickly examine greater than, less than and equal to. Given values A and B, when A is greater than B, or A is less than B, we may conclude that A does not equal B. This means that when we compare two numbers of inequaltiy, we can dismiss equality.

if A < B: print "A less than B"

elif A > B: print "A greater than B"

else: print "A equals B"

Now consider not equal to, !-, How does that differ from above? By being less selective.

A != B

is True for both cases, if and elif. So it demonstrates a non-identity, but it doesn't describe their differences. It only answers the question, "Does A equal B?" with 'yes' or 'no'.

This is an important distinction that definitely plays a role in many logical expressions, but it must be qualified as meeting the exact needs. We need to choose our expressions carefully and always work toward the one that best suits the objective.

Now let's look at how a computer can help us to make decisions when the data is not 'literal'. We would not ask a computer if 6 is greater than 5, literally. We already know the answer and we don't need a program to tell us. But what if 6 is the value 'hidden' in A, and 5 is hidden in B? This is a set of states that have arisen within the environment (data and interface) that the programmer couldn't have known, literally, when the program was coded. They are said to be dynamic.

Dynamic is what computers do best. Constant change is in their DNA, so to speak. In the above code example we have the computer describe the comparison of A to B. As you progress through your learning, try to think in terms of dynamic, and not literal so the mechanics of programming take hold. Variables and objects are easier to visualize once we begin to relate them to datum, data structures, data types and algorithms.

We can delve into the function in your assignment with the above notwithstanding. It suggests a dynamic nature immediately upon examining the first line...

def greater_less_equal_5(answer):

Here we see that answer is the dynamic entity in that we don't kinow what it represents at the time of writing the program. Our function needs to figure this out for itself. That's where the above conditional statement comes in.

We will have one literal, 5, since that is a CONSTANT in our code and remains fixed and unchanging so we can write it as the primitive value.

    def greater_less_equal_5(answer):
        if answer < 5:
            # we must subtract some number 'n' from 5 to go down the number line
            return -1
        elif answer > 5:
            # we must add some number 'n' to 5 to go up the number line
            return 1
            # no movement required
            return 0

By the above, the calling program can describe the relationship of answer to 5 in terms of a value that is suggestive of inequality or identity, AND the direction on the number line to answer from 5.


Thank you @mtf ! I was solving this question the same way as @bytejumper61088. I did not think that I needed to use answer, almost as a variable in the code. Your explanation was insightful and went above and beyond.



I got to this
if 4==5:
return 1
elif 5>=5:
return -1
return 0
It still says it is wrong with the error code:
It looks like your function output -1 instead of 0 when answer is 5. Make sure you filled in the if and elif statements correctly!


Did you find the response TL/DR? Can't help if you don't read the reply.


I read through your post like 5 times trying to make sense of it. I get some parts and others not.
What's TL/DR?


'too long, didn't read.`

In summary, use the dynamic variable, answer in your code. Don't use literals, except the 5 which is a constant in the function. Use < (less than) and > (greater than) operators, not identity operators, that is, any containing an = (equal sign).


I got it. Thanks so much!


Holy cow, this helped a bunch. Thank you for the explanation, clarification, and reasoning behind it. It makes a lot more sense when you put it the way you did.


Thank you mtf. I had the code inverted. I had if 5 < (answer) and respectively the additional lines
It should have been If (answer) < 5, or > and so on
The only problem in this kind of training is being unable to see a solution when wrong, possibly with the solution and a new set of code to solve which is similar if you weren't able to solve the first code.


Self-learning is hard, no matter who we are (with some rare exceptions). I find it difficult and I'm in my sixties. Having a dodgy memory doesn't help. Determination, persistence, sticktoitiveness and due diligence (study, follow-up, reading and lots of experimenting) are all required attributes of the self learner. We are the only ones pushing ourselves to get better.


Thank so much mtf for this explanation, which clarifies this exercise. I got stuck in this part so thanks!


3 posts were split to a new topic: Still unable to get it right