Python Forum
Misunderstanding with the “if” statement and “not equal”
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Misunderstanding with the “if” statement and “not equal”
#1
Hello everybody, I am new to Python and on this forum.

Thank you for this forum.

I don't understand why :
 
TRUE = 1
FALSE = 0
test_input = TRUE

choice = str(input("Y to confirm / N to exit: "))
It's all the time FALSE:
 
if (choice != 'Y') or (choice != 'N'):
     test_input = FALSE
and in this manner it works:
 
if (choice == 'Y') or (choice == 'N'):
     test_input = TRUE
else:
     test_input = FALSE
Thank you for your answers
Reply
#2
in the first statement:
you want both conditions to be true, so the proper code would be
if (choice != 'Y') and (choice != 'N'):
     test_input = FALSE
Reply
#3
Yes true!! Really simple! Thank you !!

Here is the code of the exercise "Calculation of a volume of a parallelepiped" with "test of the entries"!

TRUE = 1
FALSE = 0
test_measure = TRUE
test_input = TRUE

try:
    leng = int(input("length: "))
    width = int(input("Width: "))
    height = int(input("Height: "))
except:
    test_input = FALSE

while test_input == FALSE or leng == 0 or width == 0 or height == 0:

    print('Please enter a good value!')

    try:
         leng = int(input("length: "))
         width = int(input("Width: "))
         height = int(input("Height: "))
         test_input  = TRUE
    except:
         test_input = FALSE

choice1 = str(input("Y to confirm / N to exit: "))

while choice1 != 'Y' and choice1 != 'N':
     choice1 = input("Y to confirm / N to exit: ")

if choice1 == "N":
    test_measure = FALSE

while test_measure:

    print('Parallelepiped volume: ', leng * width * height)
    choice2 = str(input("Do you want to continue? (Y/N): "))

    while choice2 != 'Y' and choice2 != 'N':
          choice2 = input("Do you want to continue? (Y/N): ")

    if choice2 == 'N':
          test_measure = FALSE
    else:

          try:
               leng = int(input("length: "))
               width = int(input("Width: "))
               height = int(input("Height: "))
               test_input = TRUE
          except:
               test_input = FALSE

          while test_input == FALSE or leng == 0 or width == 0 or height == 0:

               try:
                   leng = int(input("length: "))
                   width = int(input("Width: "))
                   height = int(input("Height: "))
                   test_input = TRUE
               except:
                   test_input = FALSE

           choice1 = str(input("Y to confirm / N to exit: "))

           while choice1 != 'Y' and choice1 != 'N':
                choice1 = input("Y to confirm / N to exit: ")

           if choice1 == "N":
                test_measure = FALSE
           else:
                test_measure = TRUE

print('Good bye')
We can make the tests better??
Reply
#4
Your example should be:

def yes_no():
    while True:
        choice = input("Y to confirm / N to exit: ")
        if choice.lower() == 'y':
            return True
            # leaves the loop
        elif choice.lower() == 'n':
            return False
            #leaves the while loop
        else:
            print('Your input is invalid')
            # goes back and print again the question
The built-in function input returns a str. You don't need to convert it to str. Since Python 3, the built-in function input_raw has been converted just to input. This function does not evaluete code. In Python 2.7 the input function evaluates code, which is not good. Please use Python 3.x.. The function choice.lower() makes the str lower case. Then comparing equality to 'y' and if this conditions is not True, it compares equality to 'n' and when this is not True, the else clause is exceuted. The text will be printed and the question comes again. The return statement returns the boloean. So there is no need for a break statement inside the while True loop.

Then you can call the function:
answer = yes_no()
If you enter something different like 'Y', 'y', 'N' or 'n', it stays inside the while True loop. (if an exception has been raise, it will break also out of the loop. Like a KeyboardInterrupt / CTRL+C)

If it was successful, answer is True or False.

Aditional you should not use assigment TRUE = 1 and FALSE = 0. Use the built-in bolean data types in Python. And True == 1 == 1.0 is True :-)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Very well, thank you!!
Reply
#6
Voilà, Better!!
def f_input():

   leng = int(input("length: "))
   width = int(input("Width: "))
   width = int(input("Height: ")) 

   return leng, width, width

def ftest_try():

   try:
       leng, width, height = f_input()
       test_input = True
   except:
       test_input = False

   return test_input, leng, width, height

def ftest_answer(question, answer):

   while True:

      choice = input(question)

      if choice.lower() == 'y':
         return True
      elif choice.lower() == 'n':
         return False
      else:
         print(answer)

def ftest_input():

     test_input, leng, width, height = ftest_try()

     while test_input == False or leng == 0 or width == 0 or height == 0:

          print('Please enter a good value!')

          test_input, leng, width, height = ftest_try()

     return leng, width, height

#************ Main program **************************

choice1 = "Y to confirm / N to exit: "
choice2 = "Do you want to continue? (Y/N): "

leng, width, height = ftest_input() 
test_answer = ftest_answer(choice1, choice1)

while test_answer:

    print('Parallelepiped volume: ', leng * width * height)

    test_answer = ftest_answer(choice2, choice2)

    if test_answer:

       leng, width, height = ftest_input()
       test_answer = ftest_answer(choice1, choice1)

print('Good bye')
We can make better??

When I enter 2 or more letters in the question, for example :

“Y to confirm / N to exit: NNN
Y to confirm / N to exit:
Y to confirm / N to exit:”

The question returns twice. I change this :
def ftest_answer(question, answer):


while True:
     choice = input(question)
     if choice.lower() == 'y':
        return True
     elif choice.lower() == 'n':
        return False
     else:
        print(answer)
 
by this:

while True:
     choice = input(question)
     if choice.lower() == 'y':
        return True
     elif choice.lower() == 'n':
        return False

And now it's Ok!!
Reply
#7
You can use a hack for it.

if choice.lower()[:1]:
    #code
It returns the first element, doesn't matter how much text you enter. The first char counts. So when you enter 'ny', you get 'n'.
I think this is not a good technique.

Another approaches to get only one char: https://stackoverflow.com/questions/5103...m-the-user


Code Style
Please read the pep8. It's important to produce readable code.

Please read following:
https://www.python.org/dev/peps/pep-0008/
http://python-guide-pt-br.readthedocs.io...ing/style/

And here is a good E-Book
https://jeffknupp.com/writing-idiomatic-python-ebook/

You'll find more on the Internet.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is there equal syntax to "dir /s /b" kucingkembar 2 982 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Can a variable equal 2 things? Extra 4 1,481 Jan-18-2022, 09:21 PM
Last Post: Extra
  Misunderstanding kwargs? Donovan 2 2,268 Aug-04-2020, 08:09 PM
Last Post: Donovan
  Not equal a dictionary key value bazcurtis 2 1,917 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  misunderstanding of format in print function Harvey 2 2,175 Oct-29-2019, 12:44 PM
Last Post: buran
  Simple list comprehension misunderstanding Mark17 3 2,562 Oct-10-2019, 07:00 PM
Last Post: buran
  Bug or my misunderstanding? MrSteveVee 2 3,466 Jan-04-2018, 10:58 PM
Last Post: MrSteveVee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020