-4

i'm aware this question may look stupid but really i don't get the logic. This is really simple i want to do a program that write or read a file. So i ask the person to enter 'w' or 'r' with an input. As long as the input in not 'w' or 'r' i keep asking. the code below works and i tried diferent ways, but i don't get why it works with a 'and' as for me it should be a 'or':

re=input("type w for write , r for read\n")
while re != 'w' and  re!='r':
    re=input("type w or r")
print("ok")
eric paris
  • 61
  • 3
  • 11
  • Just use `If ` instead of `while` and change `or` instead of `and` and try . – Vikas Periyadath Jan 18 '18 at 08:51
  • 2
    This is like asking "I don't understand why 1+1 is 2 as for me it should be 5". You are just wrong and there's not much to explain... This is basic definition of what `or` and `and` are... – Julien Jan 18 '18 at 08:53
  • Evaluate the expressions and look at it again, eg if re is not w or r it results in ``while True and True:`` – Mike Scotty Jan 18 '18 at 08:53
  • 1
    This is on of [De Morgan's Laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). i.e. ~ (A v B) <-> ~A ^ ~B ... in plain english, from wolfram alpha: "it is not true that A or B is true" and "A is not true and B is not true" are equivalent. Similarly, the statements "it is not true that A and B are true" and "A is not true or B is not true" are equivalent." – juanpa.arrivillaga Jan 18 '18 at 08:56
  • This is really something you should simply work through with pen and paper. – deceze Jan 18 '18 at 09:01
  • here you are checking `!=` condition that's why you have to use `and`. If you are checking '==' then you can use `or`. got it ? – Vikas Periyadath Jan 18 '18 at 09:11
  • thanks got it. i feel i shouldn't ask these kind of questions here anyway. sorry – eric paris Jan 18 '18 at 09:14

2 Answers2

0

not (re == 'w' or re =='r') is equal to re != 'w' and re != 'r'. You want to stop (the while condition becomes false) when the input is 'r' or 'w'

marcadian
  • 2,608
  • 13
  • 20
0

Your Code could be re-written in plain English using (Psuedo-Code) like this:

1: GET INPUT FROM USER 
   INFORM THEM WE ARE ONLY INTERESTED IN "w" WHICH STANDS FOR "WRITE"
   OR "r" WHICH STANDS FOR "READ"

2: SO LONG AS THE INPUT IS "NEITHER" w NOR r, KEEP PROMPTING THE USER
   TO SUPPLY EITHER OF THE EXPECTED VALUES: "w" OR "r"
   THIS IS IMPLIED BY THE LINE THAT READS:
   while re != 'w' and  re!='r':
       re=input("type w or r")

3: HOWEVER, IF THE USER ENTERED THE REQUIRED STRING: w OR r,
   LET US JUST GO AHEAD AND PRINT OK TO THE SCREEN

In another Manner, this could also be written like so:

writeRead       = input("type w for write , r for read\n")

# SO LONG AS THE VALUE OF writeRead (re) IS NOT "w" AND IS ALSO NOT "r"
# (THIS IS WHAT YOU JUST SAID IN CODE HERE —> re != 'w' and  re != 'r': )
while writeRead not in ('wr'):     
    writeRead   = input("type w or r\n")
print("ok")

And the above snippet is "YES" functionally synonymous with:

re     = input("type w for write , r for read\n")

while re != 'w' and  re!='r':
    re = input("type w or r")
print("ok")
Poiz
  • 7,611
  • 2
  • 15
  • 17