1
\$\begingroup\$

I know the answer, I need to use an OR gate because that is how the code works. I want to learn the problem solving part of this.

The code that works is:

if(x<1 or x>10):
     print("Error")
else:
     print("Ok")

Why does it work.?

I know the truth tables of OR and AND gate as well:

enter image description here

What are we expecting in output condition in this case?

I will explain it with a figure:

enter image description here

What is the condition we are expecting there? What is needed to be either true or false? Can you clarify that much?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ You can use AND too ... if x >= 1 and x <= 10 print OK else ... This uses AND because both conditions must be true to print OK. The rules for transformation between these two forms (and note the conditions are inverted too) is called "De Morgan's law" (useful search term) \$\endgroup\$ Nov 25 at 11:17
  • \$\begingroup\$ What I am trying to figure out is the problem solving part. I understand absolutely what you are saying. But what I don't understand is how you came up with this? There must be some thinking procedure missing from my brain(Not sure about exact terminology, pardon me for that). IWTL that. \$\endgroup\$
    – zemvua
    Nov 25 at 11:58
  • \$\begingroup\$ AAAAH! Please add those remarks to the question and refine it. Because refinement is exactly how you move from loose written requirements to a precise logic that both encapsulates those requirements, and can be translated to code or logical hardware. And you're right ... it's not an easy task. There have been huge efforts (you can read about the Yourdon method, VDM (Vienna Design Method), the B and Z formal logic languages ... none of them completely successful, mostly dropped out of use. It remains an important question. Gotta run ... but deserves a good answer. \$\endgroup\$ Nov 25 at 12:34
  • \$\begingroup\$ In the absence of which ... most of it is "seen that case before, this is how it was done". Disappointing, but there we are. \$\endgroup\$ Nov 25 at 12:38
0
\$\begingroup\$

If you think about it, your number can't be smaller than 1 and larger than 10 at the same time, so both conditions will not be true at the same time, but even one of the conditions mean there is an error.

However, it can be larger than 0 and smaller than 11 at the same time, so there is no error, both conditions can be true at the same time.

\$\endgroup\$
1
  • \$\begingroup\$ i am not very clear but let me guess-:the condition that we are expecting is "error" it must be true if any one of the inputs must be true. that is how we get or gate. Am I right? \$\endgroup\$
    – zemvua
    Nov 25 at 10:45
0
\$\begingroup\$

You can also use an AND gate (a logical AND condition) by changing the code to this

if(x>=1 and x<=10)
    print "Okay"
else
    print "Error"

Your code has two conditions for an error - a number is strictly less than 1 or greater than 10. The first checks the lower bound (for 1) and the other the upper bound (for 0). Violating any of them is good enough for rejection, hence the OR Gate. Another reason why the OR gate fits in is that violating one condition is enough for us to reject it. If a number is less than 1 it is out of the range we're looking for, so we needn't even bother with checking at the upper bound. If one of the inputs of an OR Gate is High(True), the output is high irrespective of what is present at the other end.

\$\endgroup\$
0
\$\begingroup\$

Your accept condition is x >= 1 AND x <= 10, which means that your reject condition is the opposite of that: NOT(x >= 1 AND x <= 10). By applying De Morgan's Law that becomes
NOT(x >= 1) OR NOT(x <= 10), and then we can simplify that to x < 1 OR x > 10 using the definitions of the comparison operators.

As others have mentioned, you can sanity-check conditions like this easily just by picking a real value and sticking it in. If you had picked the condition x < 1 AND x > 10, a little playing should convince you that you can't find any numbers that are less than one and more than ten at the same time... and a condition that's unconditionally false isn't worth testing, so that can't be the right answer!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.