0

I don't understand why the while keeps executing even though I have written the name "gabriel" or the name "tommy"

nombre = ""
while nombre != "gabriel" or nombre !="tommy":
nombre = input()
if nombre != "gabriel" or nombre != "tommy":
    print("ingrese su nombre nuevamente")
else:
    print("su nombre es ")
    break
  • not `gabriel` or not `tommy` means anything other than those two words will trigger the while loop. In your case, in the first line, the empty string `nombre=""` means it's neither gabriel nor tommy. So the while loop is getting executed here. – Redowan Delowar Feb 09 '22 at 01:10

3 Answers3

0
nombre != "gabriel" or nombre !="tommy"

No matter what the user enters, this condition will always be true.

If they enter "gabriel", then it is not equal to "tommy".

If they enter "tommy", then it is not equal to "gabriel".

Use and instead of or.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

Below statement always return true and the loop won't stop. I guess u want to use "and" instead or in below statement.

nombre != "gabriel" or nombre != "tommy"

Jack Ng
  • 33
  • 2
0

You are using or where you should be using and. However, you are also making two checks where you only need one, and initializing nombre unnecessarily. Consider this instead:

while True:
    nombre = input()
    if nombre != "gabriel" and nombre != "tommy":
        print("ingress su nombre nuevamente")
    else:
        print("su nombre es", nombre)
        break

or using or correctly:

while True:
    nombre = input()
    if nombre == "gabriel" or nombre == "tommy":
        print("su nombre es", nombre)
        break

    print("ingress su nombre nuevamente")
chepner
  • 497,756
  • 71
  • 530
  • 681